Contact Us or call 1-877-932-8228


Variables store data that your code can use.

There are two fundamental categories of variables, primitive data and references:

  • With primitive data, the compiler names a memory location and uses itto store the actual data - numeric values such as integers, floating point values, and the code values of single individual text characters are stored as primitives.
  • With references, the data is accessed indirectly - the compiler selects a memory location, associates it with the variable name, and stores in it a value that is effectively the memory address of the actual data - in Java, all objects and arrays are stored using references.

In the diagram below, the boxes are areas in memory:

Object storage and reference variable

Declaring Variables

Variables must be declared before they are used.

A declaration informs the compiler that you wish to:

  • Create an identifier that will be accepted by the compiler within a section of code (exactly what that section is depends on how and where the variable is declared; that is the concept of scope, which will be addressed later).
  • Associate that identifier with a specified type of data, and enforce restrictions related to that in the remainder of your code.
  • Create a memory location for the specified type of data.
  • Associate the identifier with that memory location.

Java uses many specific data types; each has different properties in terms of size required and handling by the compiler:

  • The declaration specifies the name and datatype.
  • Generally the declaration also defines the variable, meaning that it causes the compiler to allocate storage space in memory of the requested type's size.
  • A declaration may also assign an initial value (to initialize the variable).
  • Multiple variables of the same type can be declared in a comma-separated list.
int a;

declares the name a to exist, and allocates a memory location to hold a 32-bit integer

int a = 0;

same as above, and also assigns an initial value of 0

int a = 0, b, c = 3;

declares three integer variables and initializes two of them

Note that different languages have different rules regarding variables that have not been initialized:

  • Some languages just let the value be whatever happened to be in that memory location already (from some previous operation).
  • Some languages initialize automatically to 0.
  • Some languages will produce a compiler or runtime error.
  • Java uses both of the last two: local variables within methods must be initialized before attempting to use their value, while variables that are fields within objects are automatically set to zero.

Advanced Declarations

Local variables, fields, methods, and classes may be given additional modifiers; keywords that determine special characteristics.

Any modifiers must appear first in any declaration, but multiple modifiers may appear in any order.

final local variables, fields, methods, classes

The name refers to a fixed item that cannot be changed.

For a variable, that means that the value cannot be changed.

For a method, the method cannot be overridden when extending the class.

A final field does not, however, have to be initialized immediately; the initial assignment may be done once during an object's construction.

static fields, methods, inner classes

Only for fields and methods of objects.

One copy of the element exists regardless of how many instances are created.

The element is created when the class is loaded.

transient fields

The value of this element will not be saved with this object when serialization is used (for example, to save a binary object to a file, or send one across a network connection).

volatile fields

The value of this element may change due to outside influences (other threads), so the compiler should not perform any caching optimizations.

public, protected, private fields, methods classes

Specifies the level of access from other classes to this element - covered in depth later.

abstract methods, classes

Specifies that a method is required for a concrete extension of this class, but that the method will not be created at this level of inheritance - the class must be extended to realize the method.

For a class, specifies that the class itself may not be instantiated; only an extending class that is not abstract may be instantiated (a class must be abstract if one or more of it's methods are abstract) - covered in depth later

native methods

The method is realized in native code (as opposed to Java code) - there is an external tool in the JDK for mapping functions from a DLL to these methods.

strictfp methods, classes

For a method, it should perform all calculations in strict floating point (some processors have the ability to perform floating point more accurately by storing intermediate results in a larger number of bits than the final result will have; while more accurate, this means that the results might differ across platforms).

For a class, this means that all methods are strictfp.

synchronized methods, code blocks

No synchronized code may be accessed from multiple threads for the same object instance at the same time.