A Java variable is an element that stores a specific type of value. It is commonly declared as a member variable within a class. A variable declared inside a class becomes an Instance variable and his associated to every instance created from that class. That is, every time a new object of that class is created, a new instance of its variable will also be created.

Alternately, a Java variable declared inside a method is not an Instance variable. That variable is local to that method only, thus the name “local variable. By default, all variables are created as instance variables.

In Java, there are multiple types of variables.

  • Instance Variables
  • Class Variables or Static Variables
  • Final Variables
  • Local Variables
  • Parameter Variables

Static 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.

To make a class or static variable, one must explicitly declare the variable as static.

Ex: static int myNumber;

Final Variables

A final variable is a variable that has been initialized to a fixed value that cannot be changed after initialization. It is a way to create a constant in Java. The final keyword is used to make a Java variable constant.

Ex: final int myNumber = 6718;

Local Variables

A local variable is declared within a method definition. Its life is confined to execution of that method.

Example:
public int add( int numA, int numB ) {
int result;
result = numA + numB;
return result;
}

In the above example, result acts as a local variable to the function add.

Parameter Variables

Similarly to local variables, parameter variables also act locally to the method to which they are passed. The only difference is that a local variable is explicitly declared inside a method, whereas a parameter variable is passed to a method from the outside.

In the above example, numA and numB acts as parameter variables.

Variable Naming Constraints

There are few rules and conventions a Java variable must follow. 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

There are few reserved words which cannot be named as a Java variable. Java variable names cannot be one of the following Java reserved words:

  • assert
  • boolean
  • break
  • byte
  • case
  • catch
  • 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