Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Variable Types in Java

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



Types of Variables in Java with Examples

A variable in Java is a named memory area which allows storing specific type of data and that data can be replaced / rewritten with new data of same type.

The types of variables in java are instance variables, static or class variables, reference variables, local or temporary variables and those can be categorised based on their declaration position, type of value they store, scope, purpose, etc.


Variable Types

Based on the types of value represented by a variable all variables are divided into 2 types.

  1. Primitive variables

  2. Reference variables


Primitive Variables

Primitive variables are used to represent primitive data type values

Example:

int i =10;


Reference Variables

Reference variables can be used to refer or point the objects.

Example:

Student s = new Student();

// here s is a reference variable.


Based on the purpose and position of declaration all variables are divided into 3 categories.

  1. Instance variables
  2. Static variables
  3. Local variables

Instance Variable

If the value of a variable is varying from object to object such type of variable is called instance variable.

For every object a separate independent copy of an instance variable will be created.

The scope of instance variable is exactly same as the scope of the objects, because instance variables will be created at the time of object creation and destroyed at the time of object destruction.

Instance variables will be stored as the part of objects.

Instance variables should be declared within the class directly but outside of any method or block or constructor.

Instance variables are also called object level variables or attributes.

Instance variables cannot be accessed from static area directly, we can access by using object reference.

From the instance area we can access instance members directly.


Example: Instance variables cannot be accessed from static method and static block directly.


class Sample
{
int x = 10;// instance variable

 // main() method
public static void main(String[] args)
{
//cannot access instance variable from static method
//System.out.println(x);// Compile time exception. non static variable x cannot be accessed from static context.

// create an object
Sample s1 = new Sample();

//access instance variable on reference variable
System.out.println(s1.x); // 10

// call a instance method m1()
s1.m1();
}
 public void m1()
{
//access instance variable x from instance method m1() directly
System.out.println(x); // 10
}
}  

Example: For the instance variables it is not required to perform initialization explicitly, the JVM will provide default values.


class Test
{
// instance variables declaration
int i;
boolean b;
String s;

 // main() method
public static void main(String[] args)
{
// create an object
Test t = new Test();
// print values of instance variables
System.out.println(t.i);// 0
System.out.println(t.b);// false
System.out.println(t.s);// null
}
}

Why can we not access the instance variables from static method or static block?

The instance variables are always the part of objects and scope of the instance variables is same as the scope of an object that means instance variables get memory space / created at the time of an object creation and will be destroyed at the time of object destruction.

The static methods and static blocks can be executed without creating objects, that is, static methods and static blocks can be executed before even creating instance variables. If java allows accessing instance variables from static context then the java applications become inconsistent because the memory allocation or object creation happens at run time, and we cannot find the problem at compile time. Therefore it is not allowed to access the instance variables from static context before creating an object.


Static Variables:

The static variables get one time memory allocation and maintains a single copy which can be shared by all the objects of the class.

Static variables will be created at the time of class loading and destroyed at the time of class unloading. Hence the scope of the static variable is exactly same as the scope of the class.

To know more, visit What is static variable in Java?


Local Variables

To meet temporary requirements, the programmer creates variables inside the method or block or constructors such types of variables are called local variables.

Local variables also known as stack variables or automatic variables or temporary variables.

The local variables will be stored inside a stack.

The local variables will be created while executing the block in which it is declared and destroyed once the block execution completed. Hence the scope of local variable is exactly same as the block in which it is declared.

The only applicable modifier for the local variables is final; you cannot use other modifiers for local variables like private, protected, public, static, etc.

Example: Scope of the local variable is within the method / block only


  class Test
{

public static void main(String[] args)
{// i is a local variable ans its scope is within main() method only
int i = 0;
// j is a local variable and its scope is within for(-) loop only
for(int j=0; j<3; j++)
{
i=i+j;
}
// j cannot be accessed outside the for loop
System.out.println("i="+i+" j= "+j);// compile time exception. Cannot find symbol j.
}
}


Example: The JVM will not provide any default values to the local variables, it is mandatory to explicitly perform local variable initialization.


class Test
{

public static void main(String[] args)
{// x is a local variable and has no explicit value assigned
int x;
if(args.length<0)
{
x = 10;
}
System.out.println(i);// compile time exception. Variable x might not have been initialized.
}
}


class Test
{

public static void main(String[] args)
{// x is a local variable and has no explicit value assigned
int x;
if(args.length<0)
{
x = 10;
}
else
{
x = 20;
}
System.out.println(x);// valid, because variable x will definitely get value 
}
}

Note:

It is not recommended to perform initialization of local variables inside logical/conditional blocks because there is no execution guarantee of these blocks at runtime.

It is highly recommended to perform initialization for the local variables at the time of declaration, at least with default value.


Example: The only applicable modifier for the local variables is final.


class Test
{

public static void main(String[] args)
{
private int x1=10; // invalid
protected int x2=10; // invalid
public int x3=10; // invalid
static int x4=10; // invalid
final int x5=10; // valid
}
}

Share the article to help your friends