Static, Instance and Local variables in java

Posted By : Rohit Godara | 30-Jul-2018

In java based on the position of declaration and functionalities we can divide variables into three categories;

a). Instance Variable: If the value of a variable is varied from object to object such type of variable is nothing but instance variable. For every object, a separate copy of instance variable is created.

For ex:

 
class InstanceVariableDemo{
			int i = 20;
			public static void main(String...args){
			   InstanceVariableDemo demo1 = new InstanceVariableDemo();
			   demo1.i = 40;
			   System.out.println("i = "+demo2.i); //Here the value of i will be = 40
			   InstanceVariableDemo demo2 = new InstanceVariableDemo();
			   demo2.i = 60;
			   System.out.println("i = "+demo2.i); //Here the value of i will be = 60
			   InstanceVariableDemo demo3 = new InstanceVariableDemo();
			   System.out.println("i = "+demo3.i); //Here the value of i will be = 20
			}
		}

Note: The instance variable should be declared within a class directly but outside of any method, any constructor and any block.

Scope Of Instance Variables;

(i).  The scope of the instance variable is exactly the same as the scope of the object because instance variables are created at the time of object creation and destroyed at the time of object destruction.

(ii). Objects are stored in heap area so that instance variables are also stored in the same memory area or in heap area.


b). Static Variable: If the value of a variable is not varied from object to object such type of variable is called a Static variable. That is, only one copy of the variable is created at the class level and shared by every object of that class.

For ex: 

 class StaticVariableDemo{
			static int i = 20;
			public stataicc void main(String...args){
			     System.out.println("i = "+StaticVariableDemo.i); // Here the value of i will be = 20
				 StaticVariableDemo.i = 40;
				 System.out.println("i = "+StaticVariableDemo.i); // Here the value of i will be = 40
			}
		}

Note: Static variable should be declared within a class directly but outside of any method, any block and any constructor.

Scope Of Static Variables;

(i). The scope of a static variable is exactly the same as the scope of the class because static variables are created at the time of class loading and destroyed at the time of class unloading. 

(ii). Classes are stored in method area so that static variables are also stored in the same memory area or method area.


c). Local Variable: Sometimes to meet a temporary requirement we can declare a variable inside a method, block or constructor such type of variable is called a local variable.

For ex:

class LocalVariableDemo{
			static int i = 20;
			for(int i=1; i<=10;i++){
				System.out.println("i = "+i); //Here the initial value of i will be 1 and than incremented by 1 upto 10.
			}
			System.out.println("i = "+i); //Here the value of i will be = 20
		} 

Scope Of Local Variables;

(i). Local variable will be created while executing the block in which we declared it. Once the block execution is completed, automatically the local variable will be destroyed. so, the scope of the local variable is the block in which it is declared.

About Author

Author Image
Rohit Godara

Rohit is always ready to face challenges and likes to work with full dedication and coordination. He is always eager to learn new technologies so as to develop his skills and knowledge.

Request for Proposal

Name is required

Comment is required

Sending message..