Java keyword static learning experience

Static is another important keyword in Java. When used in a certain environment, 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 variable modified by static is called class variable / static variable to realize the sharing of this member by all objects.

2. The member method modified by static is called class method / static method. It can be called directly through the class name without creating an object.

3. Form static code blocks to optimize program performance.

4. Static package Guide: generally, class methods are directly imported into the current class, so that class methods can be called directly using the method name, which is more convenient.

5. Modify the internal class. You can call the internal class without instantiating the external class.

A code description is attached below:

As in the above code, S1 and S2 objects are stored in different addresses of the heap area in memory, so they will not interfere with each other. However, the static keyword modifies the member variable age to make it the owner of the class rather than the object. Any object of the instance of the class can share and use it. Therefore, the output ages are finally assigned to 10. If static is removed, it is obvious that objects do not affect each other. The output is as follows:

As in the above code, an error occurred when out1 called out2. From this code, 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 the access rights in Java are private, public, protected and default.

As the above code, remember two sentences. The static code block is loaded with the loading of the class. Static modified variable methods take precedence over other variable methods and are loaded only once. When a child class is loaded, the parent class is loaded first. It can be analyzed from this: at the beginning of execution, first find the main method, because the main method is the entry of the program, but before executing the main method, you must first load the test class. When loading the test class, you find that the test class inherits from the base class, so you will load the base class first. When loading the base class, you find that there is a static block, and then execute the static block. After the base class is loaded, continue to load the test class, and then find that there are static blocks in the test class, and execute the static block. After loading the required classes, the main method is executed. When new test () is executed in the main method, the constructor of the parent class will be called first, and then its own constructor will be called. Therefore, the operation results are as follows:

As shown in the above code, 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 method of "class name. Method name" to call class methods. You can directly use "method name" to call class methods, just like the class's own methods.

As in the above code, remember to modify the internal class with static, and the external class can call the internal class directly. Because the internal class modified by static is loaded at the same time as the external class, you can call the static internal class directly without instantiating the external 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 instantiated external classes. You can directly load and instantiate inner. The operation results are as follows:

Note: another important application of static is to implement the singleton design pattern. The feature of simple interest mode is that this class can only have one instance. In order to realize this function, the constructor of the class must be hidden, that is, the constructor must be declared private and a method for creating objects must be provided. Since the construction object is declared private, the outside world cannot directly create objects of this type, and the objects of the class can only be obtained through the methods provided by the class, Therefore, you can only declare the method of creating the object as static. The program example is as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>