Static represents the meaning of "global" or "static", which modifies member variables and member methods, and can also form statically static blocks of code, but there is no concept of global variables in the Java language.
member variables and member methods that are modified by static are independent of any object of the class. That is, it does not depend on class-specific instances and is shared by all instances of the class.
As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the run-time data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.
Static member variables and member methods that are decorated with public are essentially global variables and global methods, and when you declare objects of its class, you do not generate a copy of the static variable, but all instances of the class share the same static variable.
The static modified member variables and member methods are customarily referred to as static variables and static methods, which can be accessed directly through the class name, and Access syntax is:
Class name. static method Name (parameter list ...)
A static code block is represented by a statically decorated code block, which is executed when the Java Virtual Machine (JVM) loads the class. It can also be understood that as static variables and static methods are created once loaded, there is a space in memory to store them until the closed file is cleared.
How to distinguish static variables from static methods and instance variable instance methods:
There are two ways to classify a class member variable statically: A variable that is statically modified, called a static variable or a class variable, and another variable that is not modified by static, called an instance variable.
For static variables there is only one copy in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of static variables during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through objects (but this is not recommended).
For instance variables, when an instance is not created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory without compromising (flexibility).
As a result, static variables are typically used when you need to implement the following two functions:
- When a value is shared between objects
- Easy access to variables
Static methods can be called directly from the class name, and any instance can also be called.
To add one last sentence:
Static methods can only manipulate static variables and static methods of the current class, and cannot manipulate instance variables and instance methods. The static and static methods of the current class can be called in an instance method.
The understanding of static in Java