RSS Feed

Java Variables

An instance variable is a variable that is related to a single instance of a class. Each time an instance of a class is created, the system creates one copy of the instance variables related to that class.

An instance variable is declared inside a class, but not within a method. A variable that is defined inside a class and a method is known as a local variable.

The opposite of an instance variable is a static variable, also referred to as a class variable. A static variable exists across instances of a class.

By default, all variables are created as instance variables.

Static Variablesinstance variable 150x150 Java Variables

A static variable, also referred to as a class variable, is a variable that exists across instances of a class.

The opposite of a static variable is an instance variable, which is a variable related to a single instance of a class. Each time an instance of a class is created, the system creates one copy of the instance variables related to that class.

By default, all variables are created as instance variables. To make a class variable, one must explicitly declare the variable as static.

Final Variables

A final variable is a variable that has been initialized to a fixed value that cannot be changed after initialization.

In older programming languages, a final variable would be called a constant.

Variable Naming Constraints

Java variable names must start with one of the following characters:

  • Letter
  • Underscore
  • Dollar sign

After the first character, Java variable names can contain numbers.

Java Variable Naming Reserved Words

Java variable names cannot be one of the following Java reserved words:

  • assert
  • boolean
  • break
  • byte
  • case
  • catchjava variable naming constraints Java Variables
  • char
  • class
  • const
  • continue
  • default
  • do
  • double
  • else
  • extends
  • false
  • final
  • finally
  • float
  • for
  • goto
  • if
  • implements
  • import
  • instanceof
  • int
  • interface
  • long
  • native
  • new
  • null
  • package
  • private
  • protected
  • public
  • return
  • short
  • static
  • strictfp
  • super
  • switch
  • synchronized
  • this
  • throw
  • throws
  • transient
  • true
  • try
  • void
  • volatile
  • while
Leave a Reply

Post your comments and questions below, but please follow our commenting guidelines.


Path: Home > Programming > Java > Java Variables