The static decorated member variables and member methods 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.
Need to know about JVM ClassLoader
1. Static variables
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.
The difference between the two is:
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).
The initialization order of static member variables is initialized in the order in which they are defined. Static can not modify local variables;
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
2. Static method
The advantage of a static method is that it can be called directly without generating an instance of the class, so that members that use the static modifier are no longer returned to the object so, instead of being a class that can be understood to be common, it is said that as long as the class name is accessible, there is no need to consume resources to create objects repeatedly. Because it is already in memory when the program first loads, it will not be released until the program finishes the memory. If a member is not static decorated, the memory will be recycled after use, so static should be used with caution, depending on the actual situation
If this method is used as a tool, it is declared as static and can be used without a new object, such as connecting to a database, and I declare a getconnection () method, which is defined as static, because connecting to a database is not unique to an object. It is used only as a tool to connect to a database. As for efficiency, it is not necessary to look at the usefulness of specific methods to define whether the method is static.
3. Static code block
A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each block is executed only once, so that static blocks can be used to optimize program performance.
The difference between the static method block and the static method:
Static code blocks are executed automatically;
Static methods are executed only when they are called.
Static method: If we need a method that can be called without instantiating an object when the program is written, we can use static method, the implementation is to add static before the method, as follows:
public static void Method () {}
There are several things to consider when using static methods:
In a static method, only other static members (including variables and methods) in the same class can be called directly, rather than directly accessing non-static members in the classes. This is because, for non-static methods and variables, you need to create an instance object of the class before you can use it, and the static method does not have to create any objects before it is used. (Note: A static variable is a variable that belongs to the entire class and not to an object)
Static methods cannot reference the this and Super keywords in any way, because static methods do not have to create any instance objects before they are used, and when a static method is called, the object referenced by this is not produced at all.
Static BLOCK: When a class needs to execute a program when it is loaded, static blocks can be used.
public class DemoClass {
Private DemoClass () {}
public static DemoClass _instance;
static{
if (null = = _instance) {
_instance = new DemoClass ();
}
}
public static DemoClass getinstance () {
return _instance;
}
}
Such a program executes the code in static when the class is loaded.
Java Static in-depth understanding