Translate

Saturday, March 28, 2020

Variables in java

Variables in JAVA

Types of variable


  1. Local variable
  2. Static variable
  3. Instance variable 

Local Variable 

  • Definition : Variables which are declare inside the constructor, methods and block are called Local variables.       
  • Declaration : Inside class and inside method, constructor and block.
  • Memory Allocation  : Memory allocated when method will start and destroyed when it'll completed.
  • Scope of variable  : Within method, constructor and block. 
  • Store : It store in stack memory. 
  • Example :- 
{
       void m1()
      {
             int b;           // Error (Must assign value)                                  
             int a=10;    // local variable
             System.out.println(a);   // possible
      }

      void m2()
      {
            System.out.println(a);         //not possible
      }
}
Note :- We must assign value for local variables otherwise it will give error. 


Static Variable

  • Definition : Variables which are declare inside the class but outside method, constructor  and block with static Modifier is called Static variables.
  • Declaration : Inside class but outside constructor, method and block with static modifier.
  • Memory Allocation : When .class file is loading memory allocated and when .class file is unloading memory destroyed. 
  • Scope of variable : Within the class.
  • Store : It store in stack memory.
  • Example  :-

class Test
{
      static int a = 20;  //static variable declaration
      static void m1()  //static method
     {
           Test t = new Test(); //object creation
           System.out.println(Test.a);     
           ðŸ‘†//Possible (By class)
           System.out.println(t.a);                   
           ðŸ‘†//Possible(By object)
           System.out.println(a);
           ðŸ‘†//Possible(Direct access)
     }
     void m2()                    //Instance method
     {
           Test t1 = new Test(); //object creation
           System.out.println(Test.a);       
          👆//Possible (By class)    
           System.out.println(t1.a);               
          👆//Possible(By object)       
          System.out.println(a);
          👆//Possible(Direct access)                     
     }
}
Note : In Instance and Static method static variable can be access by direct access, by using object name and by using class name. In static variable J.V.M provide default value. 


Instance Variable 
  • Definition : Variable which are declare inside the class but outside the method, constructor and block.
  • Declaration : Inside the class but outside the constructor, method and block.
  • Memory Allocation : Memory allocated when object is created and destroyed if object is destroyed. 
  • Scope of variable : Within the class.
  • Store : It store inside heap memory. 
  • Example   :- 

class Test
{
      int  a = 200;
      static void m1()
      {
              Test t = new Test();                         
              System.out.println(a);               // not possible
              System.out.println(t.a);             // possible
              System.out.println(Test.a);       // not possible
      }
      void m1()
      {
             Test t1 = new Test();                        
             System.out.println(a);                // possible
             System.out.println(t.a);              // possible
             System.out.println(Test.a);        // not possible
      }
}
Note : In instance method instance variable can be access directly and by using object reference but by class not possible. And in static method it can be access only and only by object reference. In instance variable J.V.M provides default value.

1 comment:

Constructor in Java

Rules of Constructor Class name and constructor name must be same Constructor is able to take parameters Constructor is not allowed re...