What are the characteristics of the Java static keyword?
The keyword “static” in Java has the following characteristics:
- It can modify class member variables and methods to indicate that these members belong to the class rather than an instance.
- Static member variables, when declared, are shared by all instances of a class, with only one copy existing in memory.
- Methods that are marked as static can be directly called using the class name, without needing to create an object.
- Static member variables and methods can be accessed without creating an object.
- Static member variables and methods can be accessed directly through the class name without needing an object reference.
- Static member variables and methods can be used to store and manipulate data at the class level.
- Non-static methods cannot directly access non-static member variables and methods; they can only be accessed through object references.
- The keyword “this” cannot be used in static methods because “this” refers to the current object, and static methods do not have object references.
- The static block is executed when the class is loaded, used for initializing static members, and only runs once.
- A static inner class is also static and cannot access non-static members of the outer class.