A variable stores a value of a particular type. A variable has a name, a type, and a value associated with it. In Java, variables can only store values of primitive data types and references to objects. Variables that store references to objects are called reference variables.
Variable declarations are used to specify the type and the name of variables. This implicitly determines their memory allocation and the values that can be stored in them. We show some examples of declaring variables that can store primitive values:
char a, b, c; // a, b and c are character variables. double area; // area is a floating-point variable. boolean flag; // flag is a boolean variable.
The first declaration above is equivalent to the following three declarations:
char a; char b; char c;
A declaration can also include initialization code to specify an appropriate initial value for the variable:
int i = 10, // i is an int variable with initial value 10. j = 101; // j is an int variable with initial value 101. long big = 2147483648L; // big is a long variable with specified initial value.
An object reference is a value that denotes an object in Java. Such reference values can be stored in variables and used to manipulate the object denoted by the reference value.
A variable declaration that specifies a reference type (i.e., a class, an array, or an interface name) declares an object reference variable. Analogous to the declaration of variables of primitive data types, the simplest form of reference variable declaration only specifies the name and the reference type. The declaration determines what objects a reference variable can denote. Before we can use a reference variable to manipulate an object, it must be declared and initialized with the reference value of the object.
Pizza yummyPizza; // Variable yummyPizza can reference objects of class Pizza. Hamburger bigOne, // Variable bigOne can reference objects of class Hamburger, smallOne; // and so can variable smallOne.
It is important to note that the declarations above do not create any objects of class Pizza or Hamburger. The declarations only create variables that can store references to objects of these classes.
A declaration can also include an initializer to create an object whose reference can be assigned to the reference variable:
Pizza yummyPizza = new Pizza("Hot&Spicy"); // Declaration with initializer.
The reference variable yummyPizza can reference objects of class Pizza. The keyword new, together with the constructor call Pizza("Hot&Spicy"), creates an object of class Pizza. The reference to this object is assigned to the variable yummyPizza. The newly created object of class Pizza can now be manipulated through the reference stored in this variable.
Initializers for initializing fields in objects, classes, and interfaces are discussed in Section 8.2.
Reference variables for arrays are discussed in Section 4.1.
Lifetime of a variable, that is, the time a variable is accessible during execution, is determined by the context in which it is declared. We distinguish between lifetime of variables in three contexts:
Instance variables? members of a class and created for each object of the class. In other words, every object of the class will have its own copies of these variables, which are local to the object. The values of these variables at any given time constitute the state of the object. Instance variables exist as long as the object they belong to exists.
Static variables? also members of a class, but not created for any object of the class and, therefore, belong only to the class (see Section 4.10, p. 144). They are created when the class is loaded at runtime, and exist as long as the class exists.
Local variables (also called method automatic variables)? declared in methods and in blocks and created for each execution of the method or block. After the execution of the method or block completes, local (non-final) variables are no longer accessible.