The Java Programming Idea (fourth edition) has a detailed explanation of the static keyword in P86.
"The non-static method cannot be called inside the static method, which in turn is OK." And without creating any objects, the static method is called only through the class itself, which is really an important use of the static method. ”
The following is elaborated by "1, static method, 2, static variable, 3, static code block" three aspects.
1. Static method:
Static methods are commonly referred to as static methods, and because they can be accessed without relying on any object, there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called.
However, static member methods/variables can be accessed in non-static member methods.
2. Static variables:
Static variables are also known as static variables, except that the static variables are shared by all objects, only one copy in memory, and are initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other.
3. Static code block:
Another key function of the static keyword is to form static blocks of code to optimize program performance. A static block can be placed anywhere in the class and can have more than one static block in the class. When the class is first loaded, each static block is executed in the order of the static blocks and is executed only once.
Use of the static keyword in Java