The static keyword in Java is meant to be used primarily for the following three points, static member variables, static member methods, and static blocks. In a class, if an object (variable or method) with a static type is defined, the object is instantiated when the Java Virtual Machine (JVM) loads the class , and is called directly when it is used without having to instantiate it. Often used in conjunction with Private,public. The following is a detailed look at these three parts.
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).
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
Static methods can be called directly from the class name, and any instance can also be called.
Therefore, the this and Super keywords cannot be used in a static method, and you cannot directly access the instance variables and instance methods of the owning class (that is, member variables and member methods without static), only the static member variables and member methods of the owning class.
Because instance members are associated with specific objects! This need to understand, want to understand the truth, not the memory!!!
Because the static method is independent of any instance, the static method must be implemented, not abstract.
For example, to facilitate the invocation of a method, all methods in the math class in the Java API are static, and the static method inside the generic class is convenient for other classes to call the method.
A static method is a special kind of method inside a class that declares the corresponding method static only when it is needed, and a method inside a class is generally non-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 code block is executed only once. For example:
- Public class Test5 {
- Private static int A;
- private int B;
- static{
- Test5.a=3;
- System.out.println (a);
- TEST5 t=New Test5 ();
- T.F ();
- t.b=1000;
- System.out.println (T.B);
- }
- static{
- Test5.a=4;
- System.out.println (a);
- }
- Public static void Main (string[] args) {
- TODO automatically generate method stubs
- }
- static{
- Test5.a=5;
- System.out.println (a);
- }
- Public void F () {
- System.out.println ("Hhahhahah");
- }
- }
Operation Result:
3 Hhahhahah 1000 4 5
Static code blocks allow you to assign values to some static variables, and finally take a look at these examples, all in a static Main method, so that the JVM can be called directly without creating an instance when it runs the main method.
Explanation of the role of Static in Java