Static indicates global or static values. It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables. The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class.
Static indicates the meaning of "global" or "static". It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables.
The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class. As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.
Static member variables and member Methods Modified with public are essentially global variables and global methods. When the object city of the declared class does not generate a copy of static variables, instead, all instances of the class share the same static variable.
The static variable can be modified in private, indicating that the variable can be in the static code block of the class, or other static member methods of the class (you can also use -- nonsense in non-static member methods), but it cannot be directly referenced by the class name in other classes, this is important. In fact, you need to understand that private is the access permission limitation, and static means that it can be used without instantiation, so that it is much easier to understand. The effect of adding other access key words before static is also similar.
Static modified member variables and member methods are usually called static variables and static methods. They can be accessed directly by class names. The access syntax is as follows:
Class Name. Static Method Name (parameter list ...)
Class Name. static variable name
A static code block is a static code block. When a Java Virtual Machine (JVM) loads a class, the code block is executed (very useful ).
1. Static variables
Class member variables can be classified by static or not. One is static modified variables, such as static variables or class variables; the other is a variable that is not modified by static, called an instance variable. The difference between the two is:
For static variables that only have one copy (memory saving) in the memory, JVM only allocates the memory for the static one time, and completes the memory allocation of the static variables during the loading process, you can directly access the available class name (convenient). Of course, you can also access it through an object (but this is not recommended ).
If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ).
2. Static Method
Static methods can be called directly through the class name, and can be called by any instance. Therefore, this and super keywords cannot be used in static methods, you cannot directly access the instance variables and instance methods of the class (that is, the static member variables and member methods are not included). You can only access static member variables and member methods of the class. Because instance members are associated with specific objects! You need to understand the truth, not the memory !!!
Because the static method is independent of any instance, the static method must be implemented rather than abstract.
3. Static code block
A static code block is also called a static code block. It is a static statement Block Independent of class members in a class. It can have multiple static code blocks and can be placed anywhere. It is not in any method body, when a JVM loads a class, it executes these static code blocks. If there are multiple static code blocks, the JVM executes them in sequence according to the sequence they appear in the class, and each code block is executed only once. For example:
Public class test5 {
Private Static int;
Private int B;
Static {
Test5.a = 3;
System. Out. println ();
Test5 T = new test5 ();
T. F ();
T. B = 1000;
System. Out. println (T. B );
}
Static {
Test5.a = 4;
System. Out. println ();
}
Public static void main (string [] ARGs ){
// Todo automatically generates method stubs
}
Static {
Test5.a = 5;
System. Out. println ();
}
Public void F (){
System. Out. println ("hhahhahah ");
}
}
Running result:
3
Hhahhahah
1000
4
5
Static code blocks can be used to assign values to some static variables. In the end, these examples all use a static main method, so that the JVM can directly call the main method without creating an instance.
4. What does static and final use to represent?
Static final is used to modify member variables and member methods. It can be simply understood as a "global constant "!
For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name.
For methods, it means they cannot be overwritten and can be accessed directly by class names.