Java Variables


A java variable is a container which holds the value while the java program is executed. It is a name of reserved area allocated in memory.

Variable is a combination of "vary + able" that means its value can be changed.

In Java, each variable has a specific type which is used to determine the size and layout of variable's memory.

We must declare all our variables before they can be used in our program.


Syntax:

dataType variableName;

Example:

int a = 70;

// here ‘a’ is the variable name

We can declare multiple variables of same type by a comma separated list.

Example:

int a, b, c;


Types of Variables


In Java, there are 3 types of variables:

  1. Local Variables
  2. Instance Variables
  3. Static Variables

1. Local Variable

A local variable is a variable which is declared inside the body of the method. We can use local variables only within the method in which it is declared.

We cannot use access modifiers for local variables.

We cannot define a local variable with "static" keyword.

These variables are created when the method, constructor or block is entered and will be destroyed once it exits the method, constructor, or block.


2. Instance Variable

An instance variable is a variable which is declared inside the class but outside the body of the method. It is not declared as static.

These variables are called instance variable because their value is instance specific and not shared among instances.

These variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

These variables hold value that must be referenced by more than one method, constructor or block.

We can use access modifiers for instance variables.

These variables are visible to all methods, constructors and blocks in our class.

These variables have default values. The default value for number is 0, for boolean it is false and for object references it is null.

These variables can be accessed directly by calling the variable name inside the class but within static methods, they should be called using their fully qualified name, ObjectReference.VariableName.


3. Static variable

A static variable is a variable which is declared with the static keyword in a class but outside a method, constructor or a block.

We can create a single copy of static variable and share among all instances of the class.

These variables are created when the program starts executing and destroyed when the program terminates.

These variables are stored in the static memory.

These variables are visible to all methods, constructors and blocks in our class.

Memory allocation for these variable happens only once when the class is loaded in the memory.

These variables have default values. The default value for number is 0, for boolean it is false and for object references it is null.

These variables can be accessed by calling with the class name ClassName.VariableName.

Example:

class myClass
{
    // instance variable
    int a = 10;

    // static variable
    static int b = 20;

    void method()
    {
        // local variable
        int c = 30;
    }
}