Java Reference

Java Reference Home | Smart Soft Home

Variable Lifetime

Local Variable (or Method Argument)

  • Created: When method starts
  • Destroyed: When method ends
  • Instance Variable

  • Created: When it's object is instantiated (new'd)
  • Destroyed: When it's object goes out of scope
  • Static Variable

  • Created: When process starts
  • Destroyed: When process stops
  • Example

    class MyClass{
    
      static int a;       //static variable
      int b;              //instance variable
      
      void meth1(int d){  //method argument
        int e;            //local variable
      }
    
    }