Scenario
Let's take a look at the code segment and consider the following scenarios. What is the running result?
public class Test { static int i = 8; public void printI() { int i = 88; System.out.println(this.i); } public static void main(String arg[]) { Test t = new Test(); t.printI(); }}
The final running result is:8
IfThisIf the keyword is removed, the result is88Of course, there are someThisKeyword.
Static keywords
Including usageStaticThe variables or methods declared by the keyword are not associated with the class instance objects that contain it. Because static fields or methods have been added to the memory before class instantiation, instead of allocating memory for objects declared by class instantiation. Let's look at the following code:
Class test {static int I = 8; Public void printi () {system. out. println ("I:" + this. i);} public static void main (string Arg []) {system. out. println (test. i); test. test (); test. I ++; // auto-increment I test A = new test (); // declare the test object. printi ();. I ++; // auto-increment I test B = new test (); // declare the test object B. printi ();}}
Running result:
8
I: 9
I: 10
As mentioned above, variablesIIt exists from Object Instantiation, even if we declare the object separatelyAAnd objectBAfter we perform the auto-increment operation on the I of object A, the following B object is printed.9Because the objectAAndBIs a shared variableI.
Static Method
Static methods cannot call non-static methods or variables. Otherwise, non-static methods can call static methods. For example:
int j = 10;public static void test() { System.out.println("j:"+ j);}
The compiler prompts the following error:Cannot make a static reference to the non-static field jHowever, we can easily write such code. Obviously, static methods bring great convenience to code writing. We do not need to instantiate the object when calling the method. We are encapsulating ourUtilsThis is often done in tool classes, because it is both convenient and can improve the performance of the program.Tive JavaThe static factory method is described in detail.
Static Singleton Application
As we can see in code snippet 2 above, static variables can be called without instantiation, and the modified domain is static in the memory, so for those objects that are frequently instantiated, to avoid repeated instantiation, we can achieve program performance optimization through static object. For exampleWebStatic ProjectJDBCLink objects, or objects in the business logic layer, or encapsulate factory methods, and then write a factory Singleton class. Of course we already haveSpringBy using the reflection mechanism, you can manage these bean objects in a unified manner to implement automatic on-demand injection. InPlayframeworkThis quick developmentWebIn the frameworkControllerAll methods in are declared as static methods. There are many ways to declare a singleton. Please refer to the following code:
public class Singleton {private final static Singleton INSTANCE = new Singleton();private Singleton() {}public static Singleton getInstance() { return INSTANCE;}public void test() { System.out.println("this is a test method.");}}
The constructor of this class isPrivateKeyword modification means that the object cannot be instantiated with the New Keyword. Of course, we have provided static methods in this class.GetinstanceThis method is used to obtain the static instance objects of the class. Because the object instance is saved as a static variableInstanceSo the caller calls this instance object every time. Call example:
Singleton c = singleton. getinstance (); C. Test (); // call the test method