Visitor Counter

Fat Burning

Sunday, May 31, 2015

What Is Static Keyword in Java With Example

Java allocates recollection after instantiation via new operator. But there is one exclusion to this rule, static keyword.

Static keyword is used with data and methods of the class:

  • If you want to have only a particular piece of storage for a exacting field, regardless of how many objects of that class are twisted, or even if no objects are shaped.                                    
  •  If you need a method that isn't connected with any particular object.  

static associate of the class are also referred to as class data ( and class methods) or in general class members. Class name is the favorite way to refer to static members of the class, though you can use objects as well. You can't apply static keyword to limited variables.

public class StaticDemo { 
      private static int count; // initialized to default value of int i.e. 0  
  
      public static int getCount() { 
           return count++; 
      } 
  
      public static void main(String[] args) { 
           System.out.println(" Count: " + StaticDemo.getCount()); 
           StaticDemo sd = new StaticDemo(); 
           System.out.println(" Count: " + sd.getCount()); 
      } 
 } 

production :
 Count: 0
 Count: 1
Note, primary call to method, getCount() is by Class name but the second call is during an instance of class. Output returned is dependable. This case proves a point that there is a exacting storage for static data. 

It's debatable if static methods are Object receptiveness as you call methods without creating matter. My offer, avoid using a lot of static methods.



static Initialization and static building block
Below 2 are one and same :
       private static int count = 10;  //static initialization

or

       private static int count;
       static{    //static block
          count=10;
       }      

And you can have multiple statements inside static block.
 


Order of Execution
Below case demonstrate the order of invocation :

public class StaticLoadDemo {  
      static {  
           System.out.println("Class StaticDemo loading...");  
      }  
   
      static final int c = 5; // static initialization  
      static int a;  
   
      static { // static block  
           a = c;  
           System.out.println("value :" + a);  
      }  
   
      StaticLoadDemo() {  
           System.out.println("Constructor");  
      }  
   
      public static void main(String[] args) {  
           new StaticLoadDemo();  
      }  
   
      static {  
           System.out.println("final static block");  
      }  
 }  

Output:
Class StaticDemo loading...
value :5
final static block
Constructor

Important Points:
  • Static block/initialization gets invoked correct after class gets full. Output of above class confirms the same.
  • Static block is called only one time (during the lifetime)
  • If StaticLoadDemo class extends a base class named as Base. Then static blocks of Base class will be invoked first, followed by subsequent subclasses.
  • Order of initialization is static primary, if they haven't been by now initialized by previous object creation, and then non-static component.

Inheriting static component
static components are connected with the class and not the individual object. So if a method is static, it doesn't behave polymorphic ally.

 class Super { 
      public static void printHello() { 
           System.out.println(" hello --base class"); 
      }  
 } 
  
 public class Derived extends Super { 
      public static void printHello() { 
           System.out.println(" hello --derived class"); 
      } 
  
      public static void main(String[] args) { 
           Super s = new Derived(); 
           s.printHello(); 
      } 
 } 
Output: 
hello--base class

Memory allocation and  de-allocation
static methods (in fact all methods) as well as static data are stored in the PermGen part of the heap, because they are division of the reflection data (class related data, not instance related data). 
1.            static primitive variables are stored in PermGen (Permanent Generation) space.
2.            If static data references to an Object; then reference is stored on PermGen but the actual object gets stored in heap (young/old generation).




No comments:

Post a Comment