Instance Variable vs Local Variable in java example

Java defines various  kinds of variables such as Instance Variables, Class Variables (Static Fields), Array components,  Local Variables and Parameters (Method, Constructor, Exeception) .
In this tutorial, let us understand that what is local  and instance variable?, where they do live? and what are the difference between them?. Instance Variables vs Local Variables in java is one of the popular core java interview question.
What is Instance & Local Variables ? Variables defined in a class are called instance variables.  They are  non-static fields declared within a class declaration.  Variables declared in a method by local variable declaration statements are called local variables.
Now let us see the difference between the Instance & Local Variables

1. Instance variables are declared inside a class   where as local variables are declared inside a method.
Please go through the below class Rectangle

       class Rectangle {
                   int x, y, h, w;
                   void setSize(int wP, int hP)
                        int oldW=h;
                        int oldH=w;

                 public static void main(String args[])
                         Rectangle r=new Rectangle();


In the above example , x, y, h and w are instance variables.  oldW, oldH, r  are  local variables.

2.  Instance variables or member variables   are classified as  fields  (non-static)  where as  Local variables and parameters are  classified as variables
3. An instance variable may / should  be used when the values of the variables are needed for more than one method.  Local  variables (temporary variables)  should  be used when the values are used within a single method only.  In other words,   Local variable is  visible only inside the method where it is defined and disappears once the method returns.  Instance variables are  visible  in any of the methods inside their class.  Changes in one method is reflected in other methods.  Depending upon their modifiers, they may also be accessible from outside the class.
4. Objects ( Instance variables live inside the object they belong to)  are stored in Heap    where as Local variables  are stored in Stack .   One question may arise in your mind  that  “where the local variables that are objects do live ? ”  If the local variable is a reference to an object, only the variable (the reference) goes on the stack. The actual object itself goes on heap.

The simple diagram shows , heap & stack storage for the above example class program Rectangle


 When a object is created in the program using new keyword or any other method, the object goes on Heap.    When a method is invoked,  the JVM  creates a stack frame  that stores the local variables and the parameters  for that method.   JVM maintains a stack of stack frames, with main method at the bottom, and the most recent method call on top.  As Stack works on LIFO principle,  the most recent method call is completed first, and its stack frame is popped off the stack and so on..

Note :  Instance variables  (non-static fields) are unique to each instance of a class.  When the method calls itself,  local variables in each instance of the method are given distinct addresses  in the stack.

5. Instance Variables  that are declared but not initialized will be assigned to a default value by the compiler. Default will be zero or null, depending on the data type. The following are the default values for the respective data types.

 null for  String or any other objects
 0 for byte, char, short, int, long
 0.0 for float and double
 ‘u0000’ for char
 false for boolean

For local variables, a default value is never assigned by the Compiler. If you try to use a local variable before the variable is initialized, compile-time error occurs. Local variables  must be initialized before use it
class  localVar{
public void myMethod ()
int x;
int y=10;
if ( y>20)    x = 40;
   x += 1;

public static void main(String args[])
When you run the above program, you may get the following compile-time error. variable x might not have been initialized
   x += 1;
1 error

To solve the error, Assign any value to x  before if statement OR in the else part of the  if statement

6.  Another important difference is that  Local variables are thread safe where as Instance variable are not thread safe.

Reference :  Variables


Leave a Reply