Static and javastatic Keywords of Java
Static is a keyword in Java used to modify member variables and member methods. It can also be used to compile static code blocks. When JVM loads classes, these variables are allocated a certain amount of space in the memory, that is, they are allocated space for the instances of these member variables during the compilation phase.
I. Static variables
The member variable modified by the static keyword is called a static variable. The difference between a member variable and a local variable is described earlier. What are the differences between a static variable and a member variable?
1. Call Method
Static variables: class variables, which can be called directly by class name or object name.
Member variable: instance variable, which can only be called by object name. This variable belongs to the object.
2. storage location
Static variables: static variables stored in the Method Area
Member variables: stored in heap memory
3. Lifecycle
Static variable: It exists with the loading of the class and disappears with the disappearance of the class.
Member variable: It exists with the creation of the object and disappears with the disappearance of the object
4. Correlation with objects
Static variables: data shared by all objects, with only one copy in the memory
Member variable: Data exclusive to each object. Multiple copies exist in the memory, and each copy does not affect each other.
After reading the above points, we will think about when to use static to modify our variables, we can see from 4th that when the Defined variables are the data shared by all instances, we can use static to modify them. For example, each of us has a nationality, generally, the nationality is China, so static can be used to modify it.
1 package demo; 2 3 public class Person {4 private String name; 5 private int age; 6 private static String nationality = "China"; 7 8... 9}
In this way, when constructing the Person class, you do not need to allocate memory space to the nationality attribute every time. At this time, there is only one copy in the memory, and each instance object points to it, when one of them changes the value of the nationality variable, the value of the nationality of other objects also changes.
Ii. Static Methods
The member method modified by the static keyword is called the static method. Let's look at our most common main method.
1 package demo;2 3 public class MainDemo {4 public static void main(String[] args) {5 System.out.println("main method");6 }7 }
At this time, the Code does not construct a MainDemo object, but the running program prints the main method on the console. This example shows that the static method exists better than the object, just like the static variable, it exists during class loading. If we call non-static variables and non-static methods in static methods, what will happen? Let's look at the following code.
1 package demo; 2 3 public class MainDemo { 4 private String name; 5 private static String address; 6 7 public static void main(String[] args) { 8 show1(); // Cannot make a static reference to the non-static method 9 // show1() from the type MainDemo10 show2();11 }12 13 private void show1() {14 System.out.println(name);15 System.out.println(address);16 }17 18 private static void show2() {19 System.out.println(name); // Cannot make a static reference to the20 // non-static field name21 System.out.println(address);22 }23 }
Errors are reported for lines 8th and 19th in the Code. The error message is commented out on the right of the Code. Because the static method is already in the memory when the class is loaded, the object does not exist yet, therefore, non-static variables cannot be accessed in static methods. Non-static variables exist in the heap space when an object is created. How can a non-static variable be accessed, the reason why non-static methods cannot be accessed in static methods is the same. Since the non-static method is shared by all instances, this method is also owned by the class, which can be called by the class name.
1 package demo; 2 3 public class MainDemo { 4 private static String address; 5 6 public static void main(String[] args) { 7 MainDemo.show(); 8 } 9 10 private static void show() {11 System.out.println(address);12 }13 }
3. Static code block
The static block can be placed anywhere in the class, and there can be multiple static blocks in the class. Like member variables and member methods, static code blocks exist in the memory when the class is loaded. When the class is first loaded, JVM executes each static block in the order of static blocks, and only executes the block once, which virtually improves the performance.
1 package demo; 2 3 public class StaticDemo { 4 private static String address = "china"; 5 6 static { 7 System.out.println("first static block"); 8 } 9 10 static {11 System.out.println("second static block");12 }13 14 public static void main(String[] args) {15 StaticDemo.show();16 }17 18 private static void show() {19 System.out.println(address);20 }21 }
Output result:
first static blocksecond static blockchina
From the above, we have summarized some characteristics of the following static keywords:
1. Static can be used to modify member variables, member methods, and static code blocks;
2. Loading with classes takes precedence over objects. objects are shared by all objects and can be directly called by class names (recommended). Of course, they are also called through instances, although there is only one copy in the memory, but the lifecycle is long, when the class is recycled, the static zone space will be released;
3. Only static variables and static members can be accessed in static methods;
4. When all objects share a data, the member variable is defined as static;
5. The static code block is executed only once.