Difference Between Local Variable And Instance Variable

As we go deep into java the question arises is what is the difference between local variable and instance variable? In this post I am trying to explain the difference between them.

In my last post here I have shown different types of variables. In this post we will check what is the main difference between Instance and Local variable.
Instance Variables
Instance variables are declared inside a class but not inside a method.

class MyClass{
int rollNumber;//instance variable
}
Local Variables
Local variables are declared inside a method including method arguments.

public void sum(int a,int b)
{
int x;
x=a+b;//x,a,b are local variable
}
Internal structure and work done by java
When java sees a method with arguments and local variables, It creates a stack. A frame will be created from the method. In the same stack,a variable or variables are placed.

public void sum(int a,int b)
{
int x;
x=a+b;//x,a,b are local variable
}
for this code the stack will look like-


Stack Heap Comments
sum,x,a,b No heap entry Creates a frame in the stack having the method name
Now if the method sum ,calling another method display(), then one more entry will be created on top of sum().
Code:

public void sum(int a,int b)
{
int x;
x=a+b;//x,a,b are local variable
display(x);
}
public void display(int i)
{
Account f=new Account();
f.add(i);
}
Account class structure:

class Account{
int a,b;
}
The stack and heap view will be:


Stack Heap Comments
display,x f(Account)
a=0
b=0
Since f is an instance of Account class, the object will go to heap.a and b
being Instance variable will go to heap and gets initialized to zero.
Account() the constructor will create a block in the heap and provides name as reference.
sum,x,a,b No heap entry Creates a frame in the stack having the method name
The assignment '=' will make a reference variable point to the object in heap.During execution once the display() method is completed,display is flushed from the stack.Then the sum() will be executed. Once that is over the stack is flushed again.All the variables are vanished for the respective methods once the stack is flashed. This is how the scope is defined. Once the display() is over, there is no active reference variable pointing to heap's reference variable or block.The block is eligible for garbage collection.



Local Variable Instance Variable
Local variable will not get any value bydefault Depending on the data type Instance variable will get default value.Like-bollean will get false
int will get 0 etc
The scope of local variable is very limited ..only inside the block (function) The scope is global
It is mandate to initialize the local variable to performa any action As by default it will get some value hence it is not mandate to initialize any value to the variable.
Mostly we will get compile time error if something goes wrong with Local variable Mostly we will get run time error if something goes wrong with Instance variable



What happens when one object is calling another object

class MyClass{
public static void main(String args[]){
A parent=new A();
}
class A{
B child=new B();
int e;
}
class B{
int c;
int d;
}
 
The equivalent structure will be:

Stack Points to Heap Points to Heap
parent ----> child ----> c=0
d=0
main e=0
So we can say in the heap, there is a stack created for the objects and variables.
Points to remember:

  • When a method is called a frame is created on the top of the stack.
  • Once execution is completed for a method,the control returns to the calling method.The corresponding stack is flushed.
  • Local variables are created inside the stack. They are vanished once the stack is flushed.
  • Instance variables are created in a Heap and they are part of the object they belong to.
  • Reference variables are created in the stack. 

Difference Between Local Variable And Instance Variable Difference Between Local Variable And Instance Variable Reviewed by Animesh Chatterjee on March 30, 2013 Rating: 5

No comments:

Powered by Blogger.