Static

Compiled By Unknown - No Comments

Explain static in java.

  • A static variable is attached with the class as a whole and not with specific instances of a class.
  • Each object shares a common copy of the static variables which means there is only one copy per class, no matter how many objects are created from it. 
  • Static variables are declared with the static keyword in a class. 
  • Static variables are always called by the class name. 
  • It is created when the program starts and gets destroyed when the programs stops. 
  • Static methods are implicitly final, because overriding is done based on the type of the object
  • Static methods are attached to a class, not an object. 
  • A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. 
  • You can’t override a static method with a non-static method which means you can’t change a static method into an instance method in a subclass.
  • Non-static variables has unique values with each object instance.

    When is static variable loaded? Is it at compile time or runtime?  

  • Static variable are loaded when classloader brings the class to the JVM.
  • It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded.
  • The code in a static block is loaded/executed only once i.e. when the class is first initialized.


  • Tags:

    No Comment to " Static "