Difference between Instance variables and Local variables

Instance variables are declared inside a class but not inside a method

    class Student{ int num; // num is instance variable public void showData{} Local variables are declared inside a method including method arguments. public void sum(int a){ int x = int a + 3; // a , x are local variables }

Points to Remember: 

  1. When a method is called , a frame is created on the top of stack. 
  2. Once a method has completed execution , flow of control returns to the calling method and its corresponding stack frame is flushed. 
  3. Local variables are created in the stack Instance variables are created in the heap & are part of the object they belong to. 
  4. Reference variables are created in the stack.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email