Types of variables in Java

There are three different types of variables in Java.

  • Local variable
  • instance variable
  • static variable
  • reference variable

Local variables

Variables that are declared inside a method or loop are said as local variable.

The scope of a local variable is within the loop or method in which it is declared.

It allocates memory temporarily till the loop or function is complete.

Example 1

void add(){

int a=5; //local variable

}

Variables that are declared in parameter's list of methods is also said as local variable.

Example 2

void add(int i)

// i is also the local variable.

}

Example 3

for(int i=0;i<10;i++){

// i is also the local variable.

}

Example 4

for(i=0;i<10;i++){

int g=15;//local variable.

}


Instance variables

Non-static variables that are declared inside a class are said as instance variable.

These are called as instance variables since it is unique to each object or instance of a class.

The scope of instance variable is within the class and belongs to that object only.

Example

class InstanceVariableTest{

int data; \\instance variable

float seconds;

InstanceVariableTest(int value,float sec){

data=value;

seconds=sec;

}

public static void main(String args[]){

InstanceVariableTest ct=new InstanceVariableTest(5,1.2f);

System.out.println("Default value of variable 'seconds' "+ct.seconds);

}

}

Try In Editor

Output

Default value of variable 'seconds' 1.2


Static variables

Variables that are declared with static keyword inside a class are said as static variables.

static keyword tells the compiler there exists only one copy of the variable.

static variables memory allocation does not depend on the object creation and memory is allocated before main method is called.

The scope of instance variable belongs to the class and does not belong to the specific object.

Example

class StaticVariableTest{

static int data=15;\\static variable

public static void main(String args[]){

StaticVariableTest svt1=new StaticVariableTest(),svt2=new StaticVariableTest();

svt1.data=12;

System.out.println("Value of variable 'data' "+svt2.data);

}

}

Try In Editor

Output

Value of variable 'data' 12

Look out the above example, we initialized 'data' variable of svt1 to 12.

When we print 'data' variable of svt2 object, it prints the overwritten value 12.

This is static variable. i.e the scope belongs to class not the specific object.


Reference variables

Variables that are only used to reference a object is know as reference variable.

It does not allocate memory and just it stays as a reference for a object created.

The scope of reference variable is within the function or loop from where it is declared.

Example

class StaticVariableTest{

static int data=15;

public static void main(String args[]){

StaticVariableTest svt1=new StaticVariableTest(),svt2=new StaticVariableTest();

svt1.data=12;

System.out.println("Value of variable 'data' "+svt2.data);

}

}

Try In Editor

Output

Value of variable 'data' 12

svt1 and svt2 of class StaticVariableTest are said as reference variable in the above example.


What is a variable or datatype and why they are used detailly ?