This section describes the use of the Java keyword static

Hello, this is Unconventional. After the final keyword, today introduces the static keyword. The static keyword means static. In Java, modifying variables (not local variables, local variables expire at the end of a method, static modifiers are global), methods, constructing static code blocks, modifying classes (inner classes), and static packages (less readable) after JDK1.5. Let’s introduce them separately.

1. Modify variables

When static modifies a variable, it can also be called a class variable or static variable. Unlike instance variables that are not static, class variables are classes and are common to all objects. Instance variables, on the other hand, belong to the object and are private to that object. So how do you understand that? Before JDK8, static variables were stored in the permanent layer. After JDK1.8, static variables were stored in the heap. ** During class loading, static variables are already generated and unique. ** For example, we have an Animal class with a static variable name and a non-static variable weight. New A1 and a2 are two Animal objects. The variable attribution is shown as follows:

That is, static variables in an object point to a common static storage area in the heap. So static variables are independent of objects and can use class names. Call a static variable directly, no matter how many objects there are in the class.

Two, modification method

A static modifier is similar to a variable modifier. There is only one copy of both the static method and the instance method, and no matter how many objects a class has, it shares the same method. Static methods do not need to create an object, and can pass the class name directly. Method. You can also go through objects. Method, while instance methods must first create the object, and then pass the object. Method. 2. The this keyword cannot be used in static methods (this keyword is instance-dependent). This keyword will be covered later. 3. Static methods cannot reference non-static variables. Because non-static variables are created when the object is created, static resources are already available when the class is initialized and cannot be identified. Both static and instance methods can refer to static variables.

Static code blocks

Code blocks decorated with static are static code blocks that are initialized during the preparation phase of the linking phase when the class is first loaded. Static code blocks can be multiple, and static variables are loaded in declarative order and only once, taking precedence over other non-static resources. You can optimize program performance by taking advantage of the fact that static code blocks are loaded only once. When the class requires a large configuration file to be executed at object creation time, the configuration file can be placed in a static code block. Static resources are loaded in declarable order, so if a static variable is placed after a code block that needs to reference the static variable, an error will be reported. 2. Static code blocks cannot be placed in any method, otherwise an error will be reported. Because both class and instance methods need to be called manually, static resources are loaded at class load time.

4. Modifier class

When static modifies a class, the class is a static inner class. The main function of static inner class is that the external class does not need to create an inner class when calling the inner class. Access the inner class by way of the inner class name. For example, CallerRunsPolicy, AbortPolicy, DiscardPolicy, and DiscardOldestPolicy in ThreadPoolExecutor are static inner classes.

Five, static guide package

After JDk1.5, use import static package name instead of import package name. These two keywords can be used together to specify the import of a specified static resource in a class without using the class name. Resource name. You can use the resource name directly. Such as:

//import static java.util.Arrays;
// Notice the * here, which means importing all the static methods in the Arrays class, or turning the * into concrete method names.
import static java.util.Arrays.*;

public class A
{
    public static void main(String[] args)
    {
        int[] arrays = {1.2.3.4.6.7.9.10};
        //Arrays.sort(arrays);
        sort(arrays);// Can be called without the class name}}Copy the code

However, this only reduces the amount of code, does not improve performance, and poor readability, generally not recommended to use, test can simplify the amount of code to use.

We will share what we have learned and what we have acquired at the unconventional meeting. CSDN address: blog.csdn.net/kaneandblan…