There is no concept of global variables in Java, but we can implement a "pseudo-global" concept through static. In Java, static means "global" or "Static" means, used to modify member variables and member methods, of course, it can also modify code blocks.
Java divides memory into stack memory and heap memory. Stack memory is used to store some basic types of variables, arrays and object references, and heap memory mainly stores some objects. When the JVM loads a class, if the class has statically modified member variables and member methods, a fixed-size memory area will be opened for these member variables and member methods at a fixed location. With these "fixed" features , then the JVM can access them very conveniently. At the same time, if static member variables and member methods do not go out of scope, their handles will remain unchanged. At the same time, the concept of "static" contained in static means that it is irrecoverable, that is, if you modify it at that place, it will not change back to its original state. If you clean it up, it will not come back.
The member variables and member methods that are statically modified at the same time are independent of the class. It does not depend on a specific instance variable, which means that it is shared by all instances of the class. The references of all instances point to the same place, and modifications to any instance will cause changes to other instances.
public class User { private static int userNumber = 0 ; public User(){ userNumber ++; } public static void main(String[] args) { User user1 = new User(); User user2 = new User(); System.out.println("user1 userNumber:" + User.userNumber); System.out.println("user2 userNumber:" + User.userNumber); } } ------------ Output: user1 userNumber:2 user2 userNumber:2
Static can be used to modify member variables and member methods. We call them static variables and static methods, which can be accessed directly through the class name.
ClassName.propertyName
ClassName.methodName(……)
The code block modified by Static represents a static code block. When the JVM loads the class, this block will be executed. Code is very useful. (For the introduction of the use of code blocks in the past few days, please pay attention)
2.1. Static variables
Variables modified by static are called static variables, and variables that are not modified with static are called variables. are instance variables. The difference between them is:
Static variables are initialized when the class is loaded. There is only one of them in the memory, and the JVM will only allocate memory for it once. At the same time, All instances of a class share static variables, which can be accessed directly through the class name.
But the instance variable is different. It is accompanied by the instance. Every time an instance is created, an instance variable is generated, which lives and dies with the instance.
So we generally use static variables in these two situations: data sharing between objects and easy access.
2.2. Static method
The static modified method is called a static method, and we call it directly through the class name. Since it exists when the class is loaded, it does not depend on any instance, so the static method must be implemented, which means that it cannot be an abstract method.
Static method is a special method in a class. We only declare methods as static when we really need them. For example, all methods of the Math class are static.
2.3. Static code block
The code block modified by static is called a static code block. The static code block will be executed as the class is loaded, and it can be placed at will. Can exist anywhere.
Static does have many functions, but it also has some flaws.
1. It can only call static variables.
2. It can only call static methods.
3. This and super cannot be referenced in any form.
4. Static variables must be initialized when they are defined, and the initialization time must be earlier than non-static variables.
Summary: Whether it is a variable, method, or code block, as long as it is modified with static, it will be "ready" when the class is loaded, that is, it can be used or has been executed, and can be detached. object and executed. On the contrary, if there is no static, it must depend on the object instance.
#Attention, students who are learning Java! ! !
If you encounter any problems during the learning process or want to obtain learning resources, you are welcome to join the Java learning exchange group: 159610322 Let’s learn Java together!
The above is the detailed content of Detailed explanation of keyword static instances in Java. For more information, please follow other related articles on the PHP Chinese website!