How to use Static in Java
This article is about how to use Static in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Literal meaning of static: static
What static means in the program, static, does not need to be instantiated
Static is an adjective that means to modify something, including {methods, variables, code blocks}
The method modified by static:
You can use the class name directly. Method name invocation does not require class instantiation.
Static decorated methods cannot contain anything non-static. This contradicts the definition of static.
It is possible to call static methods in non-static methods, and vice versa.
Variables modified by static:
The static variable is shared by all objects, has only one copy in memory, and is initialized when and only when the class is first loaded. Non-static variables are owned by the object, are initialized when the object is created, and there are multiple copies, which are not affected by each copy.
The example of static
The main method, the main method is the entry for the function to be called by the system, and if the main method needs to be called after instantiation, it cannot be used.
two。
From the following example, you can see that the object can be accessed directly in StaticModifierTest, and the object is assigned a value of 10
StaticModifier.java
Package cp5.zgthlife.cn
Public class StaticModifier {
Public static int number = 0
}
StaticModifierTest.java
Package cp5.zgthlife.cn
Public class StaticModifierTest {
Public static void main (String [] args) {
StaticModifier.number = 10
System.out.println (StaticModifier.number)
}
}
Thank you for reading! This is the end of the article on "how to use Static in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!