Static is another important keyword in Java. It can improve the running performance of the program and optimize the structure of the program. Its main applications are as follows:
1. The member variables modified by static, called class variables/static variables, to realize the sharing of all objects to that member.
2. The member method modified by static, called class method/static method, can be called directly through the class name without creating an object.
3. Form static code blocks to optimize program performance.
4. Static lead packages, generally import class methods directly into the current class, so that the class methods can be called directly using the method name, which is more convenient.
5. Modify the inner class, and call the inner class without instantiating the outer class.
The following code description is attached:
public class Student { String name; static int age; public String toString() { return "Name:" + name + ", Age:" + age; } public static void main(String[] args) { Student s1 = new Student(); s1.name="Zhang San"; s1.age=20; Student s2 = new Student(); s2.name="Li Si"; s2.age=10; System.out.println(s1); System.out.println(s2); }}/**Output result Name: Zhang San, Age:10Name:Li Si, Age:10*/As mentioned above, the two objects s1 and s2 are stored in different addresses in the heap area in memory, so they will not interfere with each other. However, the static keyword modifies the member variable age, making it the class belongs to rather than the object belongs to it, and any object in the instance of that class can be shared and used. Therefore, the output ages are all final assigned to 10. If you remove static, obviously the objects will not affect each other, and the output is as follows:
Name: Zhang San, Age:20Name: Li Si, Age:10
As mentioned above, an error occurred when out1 calls out2. From this code, it can be seen that static methods cannot call non-static member variables and member methods. Non-static methods can call static member variables and member methods. Note that static does not affect the access rights of members. The keywords that can affect access rights in Java are private, public, protected, and default.
public class Test extends Base{ static{ System.out.println("static static block 1"); } public Test(){ System.out.println("test constructor"); } public static void main(String[] args) { new Test(); }} class Base{ static{ System.out.println("static static block 2"); } public Base(){ System.out.println("base constructor"); }}As mentioned above, remember two sentences, the static code block is loaded with the loading of the class. The variable method modified by static is loaded first and only once than other variable methods. When a subclass is loaded, the parent class will be loaded first. From this we can analyze: at the beginning of execution, we must first find the main method, because the main method is the entrance to the program, but before executing the main method, we must first load the Test class. When loading the Test class, we find that the Test class inherits from the Base class, so we will turn to load the Base class first. When loading the Base class, we find that there is a static block, and then execute the static block. After the Base class is loaded, the Test class continues to load, and then finds that there are static blocks in the Test class, so the static block is executed. After loading the required class, the main method begins to be executed. When executing new Test() in the main method, the constructor of the parent class will be called first, and then the constructor of its own. Therefore, the operation results are as follows:
static static block 2static static block 1base constructor test constructor
/* A.java file*/package com.dogo.test;public class A { public static void outWord(Object o){ System.out.println(o); }}/* B.java file*/import static com.dogo.test.A.*;public class B{ public static void main( String[] args ) { outWord("Hello World!"); } /**Output* Hello World! */}As mentioned above, A.java contains a simple static method. In B.java, we use the static keyword to import Class A. Therefore, there is no need to use the "class name. method name" to call the class method. You can directly use the "method name" to call the class method, just like the class's own method.
/*Outer.java*/public class Outer { static { System.out.println("Outer static block"); } public Outer(){ System.out.println("Outer constructor"); } static class Inner{ static{ System.out.println("Inner static block"); } public Inner(){ System.out.println("Inner constructor"); } }}/*Test.java*/public class Test{ public static void main(String[] args) { new Outer.Inner(); }}As mentioned above, remember to modify the inner class with static, the outer class can directly call the inner class, because the inner class modified by static is loaded while loading the outer class, so you can directly call the static inner class without instantiating the outer class. In the example, before entering the main method of Test, load the Test class, and then execute new Outer.Inner(); Note here: Because Inner is static, there is no need to load external classes and instantiate external classes here. Inner can be loaded and instantiated directly. The operation results are as follows:
Inner Static Block Inner Constructor
Da Qiao Note: Another very important application of static is to implement the singleton design pattern. The characteristic of the single-interest pattern is that the class can only have one instance. In order to realize this function, the class constructor must be hidden, that is, the constructor is declared as private and provides a method to create an object. Since the constructor is declared as private, the outside world cannot directly create an object of this type, and can only obtain the class object through the methods provided by the class. Therefore, the method of creating an object can only be declared as static. The program example is as follows:
class SingleDemo{ private static SingleDemo ins=null; private SingleDemo(){} public static SingleDemo getIns(){ if(ins==null){ ins=new SingleDemo(); } return ins; } }