Skip to content
Related Articles

Related Articles

Improve Article

Difference between Instance Variable and Local Variable

  • Last Updated : 31 May, 2021

A variable is a name given to a memory location. It is the basic unit of storage in a program.

  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location. All the operations are done on the variable effects of a memory location.
  • In Java, all the variables must be declared before use.

Instance Variable: These variables are declared within a class but outside a method, constructor, or block and always get a default value.

  • These variables are usually created when we create an object and are destroyed when the object is destroyed.
  • We may use an access specifier, for instance, variable, and if no access specifier is specified, then the default access specifier is used.
  • Each and every object will have its own copy of instance variables.

Example:

class Taxes  
{  
   int count; // Count is an Instance variable  
   /*...*/  
}  

Local Variable: These variables are declared within a method but do not get any default value.

  • They are usually created when we enter a method or constructor and are destroyed after exiting the block or when the call returns from the method.
  • Its scope is generally limited to a method and its scope starts from the line they are declared. Their scope usually remains there until the closing curly brace of the method comes.
  • The initialization of the local variable is mandatory.

Example:



int area()      
{    
        int length = 10; // Local variable          
        int breadth = 5; // Local variable          
        int rectarea = length*breadth; // Local variable          
        return rectarea;          
}  

Tabular difference between the instance variable vs local variable:

Instance Variable 

Local Variable 

They are defined in class but outside the body of methods.  They are defined as a type of variable declared within programming blocks or subroutines. 
These variables are created when an object is instantiated and are accessible to all constructors, methods, or blocks in class. These variables are created when a block, method or constructor is started and the variable will be destroyed once it exits the block, method, or constructor.
These variables are destroyed when the object is destroyed.  These variables are destroyed when the constructor or method is exited.
It can be accessed throughout the class.  Its access is limited to the method in which it is declared.
They are used to reserving memory for data that the class needs and that too for the lifetime of the object.They are used to decreasing dependencies between components I.e., the complexity of code is decreased.
These variables are given a default value if it is not assigned by code.       These variables do not always have some value, so there must be a value assigned by code.
It is not compulsory to initialize instance variables before use.  It is important to initialize local variables before use.
It includes access modifiers such as private, public, protected, etc.  It does not include any access modifiers such as private, public, protected, etc.
My Personal Notes arrow_drop_up
Recommended Articles
Page :