Sunday, 17 May 2020

Difference between local and instance variable in java:

Difference between local variable and instance variable in java.


Here are some difference between instance variables and local variable in java programming language. The differences is in terms of access, scope, place of declaration etc.

What is Local variables?
    The variables declared inside the method of a class is called local variables. we can access these variables within that method only.
    we cant access these variables in other methods of that class.this keyword is not applicable for local variables.

What is Instance variables?
    The variables declares  outside the method and inside the class are called instance variables.
    we can access these variables in any method of that class.

In java programming language we have a keyword name this which is to differentiate between instance variables and parameters,  or instane variables and local variables.
 when local variables/parameter names and instance variables have same name to differentiate them we use this keyword.
  this keyword is applicable to instance variables only .it is not applicable for local variables and parameters.
     syntax of using this keyword
                    this.<variable_name>;

If we wont use this keyword conflict will come as names are same.
when we call any method and in this method if we use any variable control first searches for that variable within that method (i.e; local variable ) if available ok otherwise it goes to instance variable.
suppose we a local variable and instance variable with same name inside a method, and if we want to use the value of instance variable without specifying the this keyword it will use the value of local variable.
Hence to use instance variable value we use this keyword.

Below is one example on this keyword:

class Example{
   int variable1 ;
   void display(int variable1){
        int variable2 =100;
        this.variable1 =variable1;
        System. out. println( variable1 + " is value of instance var");
        System. out. println( variable2 + " is value of local var");
       
   }

}
class Examplemain{
        public static void main( String args[] ){
          Example e=new Example(10);
          e.display();
          }
         }
       
       
output of above program:
    10  is value of instance var
    100 is value of local var

Example 2:
   Program without this keyword
  
   class Example2{
   int var1 ;
   void display( ){
        int var1 =500;
        System. out. println( " ins var "+var1 );
        System. out. println( "  local var" + var1);
       
   }

}
class Examplemain{
        public static void main( String args[] ){
          Example2 e=new Example2();
          e.display();
          }
         }
       
       
output of above program (which does not has this keyword):
    ins var 500
    local var 500



Example 3:
Below is another example on this keyword:
   
   
    class swapping{
     int variable1, variable2;
     void accept( int v1, int v2){
         this. variable1 =v1;
         this. variable2 =v2;
     }
   
     void display(){
        System. out. println( variable1 + "   " + variable2);
     }
     void swap( ){
          int variable3;
          variable3 = variable1;
          this .variable1 = variable2;
          this .variable2 = variable3;
     }
    }
   
    class swapmain{
        public static void main( String args[] ){
          swapping s=new swapping();
          s. accept(10,20);
          System .out .println(" before swapping values are ....");
          s.display();
          // calling swap method to perform swapping
          s. swap();
          System. out. println(" after swapping values are ....");
          s.display();
        }
    }
   
 save the above program by name swap.java and compile and run to see the output.

 Compile:
     c:\>  javac swap.java
 Run:
     c:\> java swapmain
   
output:
    before swapping values are ....
    10  20
    after swapping values are ....
    20 10

 In the above program variable1 and variable2 are called as instance variables. variable3 is declared inside swap method hence it is called as local variable, we can use this variable (variable3 ) with in that method only, cant access it outside the method.
 we cant apply this keyword for variable3.

There will be one copy of instance variable per object. These variables will be there as long as object is there.
whenever we call a method, method will execute and memory will be allocated for local variable and will be there as long as method is executing.

No comments:

Post a Comment