Variables in Java

Java variables can be considered as a container which is used for holding any kind of values. It is a must to declare a variable so that it uses a datatype which includes any one of those 8 datatypes. Java supports 6 types of variable.

  • Local variable
  • Static variable
  • Instance or non-static variable
  • Final variable
  • Transient variable
  • volatile variable

Local Variable

If the variable is declared within a constructor, block or method then it is called local variable.

public class Test {
	int x=100; //not a local variable, Instance variable
	{
		int a ; //local variable, In a block
	}
	public Test()
	{
		int b=90; //local variable, inside constructor
	}
	public static void main(String[] args) {
		
		int c=200; //local variable inside a method.
	}

}
  • Since local variables can only be accessed within the same block in which it is declared, so it never contains any access specifier.
  • Local variables cannot be declared as a static variable because a static variable is a part of Java class.
  • Before using a local variable make sure that variable is initialized.
  • Local variables can be accessed only directly and not by class or object name.
  • All parameter of any function is a local variable.
void show(int x) //x is a local variable
	{
	
	}
for(int i=0;i<10;i++) //i is a local variable present in an inline declaration
{
}

Static Variable

  • If a variable is declared with a class but outside of constructor, method and block with a static keyword then it is known as a static variable in java.
  • A static variable is not a part of Java object so it can be accessed by class name.
  • Static variable allocates memory for a single time, in other words, it creates a single copy for all objects. 
  • Static variables can be accessed by class name, object name or directly within the same class.
  • Static variables can only be accessed directly within the same class.
  • The default value of a static variable is the default value of the datatype assigned.
public class Test {
	static int x=100;
	public static void main(String[] args) {
		
		Test obj1= new Test();
		Test obj2= new Test();
		System.out.println(obj1.x+" "+obj2.x); // 100 100 
		//accessing static variable using object reference.
		obj1.x=200;
		System.out.println(obj1.x+" "+obj2.x); //200 200
	}

}

Instance variable

  • This variable is otherwise known as a non-static variable. When a variable is present outside of any block without using any static keyword then it is known as an Instance variable.
  • An instance variable is purely a part of Java object so it can only be called by object name.
  • Instance variable creates different copy for a different object.
  • Instance variable by default holds the default value of the datatype assigned.



public class Test {
	int x=100;

	public static void main(String[] args) {
		
		Test obj1=new Test();
		Test obj2=new Test();
		System.out.println(obj1.x+" "+obj2.x); // 100 100
		
		obj1.x=200;
		System.out.println(obj1.x+" "+obj2.x); //200 100
	}

}

From the above program, we can see that the instance variable x creates different copies for a different object, obj1, and obj2.

java variables

Final Variable

  • If a variable is declared with the final keyword then it is known as a final variable. A final variable may or may not be static. But it is always better for a programmer to declare final variables as static.
  • The value of a final variable cannot be changed just like a constant and at the time of declaring the variable should be initialized.
public class Test {
	 final int x=100;
	 final static int y =200;

	public static void main(String[] args) {
		
		Test obj1=new Test();
		Test obj2=new Test();
		System.out.println(obj1.x+" "+obj2.y); // 100 200
		
		y=90; //Error, as final values cannot be rewritten.
	}

}

Transient Variable

A transient variable is a variable whose serialization cannot be done. The transient keyword can be used to indicate the Java Virtual Machine that the variable is not part of the persistent state of the object. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.

Volatile Variable

Declaring a volatile Java variable means the value of the variable will never be cached thread-locally; all reads and writes will go straight to "main memory". Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself. Using volatile is yet another way of making class thread safe. Thread safe means that a method or class instance can be used by multiple threads at the same time without any problem.