Static in java

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.
Garbage collection mechanism in Java
The purpose of garbage collection is to identify and remove objects that are no longer needed by a program...
Can Abstract Class have constructors?
Abstract class can have a constructor. But as we can't instantiate abstract class, we can't access it through the object...
How Java addresses the issue of portability and security
Addressing Portability: Many computers of different types with several operating systems are connected to network / internet...
Post your comment