Explain static in java.1. A static variable is attached with the class as a whole and not with specific instances of a class.
2. 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. 3. Static variables are declared with the static keyword in a class. 4. Static variables are always called by the class name. 5. It is created when the program starts and gets destroyed when the programs stops. 6. Static methods are implicitly final, because overriding is done based on the type of the object. 7. Static methods are attached to a class, not an object. 8. 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. 9. 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. 10. Non-static variables has unique values with each object instance. When is static variable loaded? Is it at compile time or runtime? 1. Static variable are loaded when classloader brings the class to the JVM. 2. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. 3. The code in a static block is loaded/executed only once i.e. when the class is first initialized.
|