Keyword static for Java learning

static

The keyword static can be used to modify methods, variables, and code blocks. Variables or methods defined by static can be directly referenced by the class name.

static code block

A static code block is also a static block. It can be placed anywhere in the class (except inside the method), and the class can have multiple static blocks. When the JVM loads a class, the static data is executed only once when the class is loaded (generating the .class file), and the same is true for static code blocks. All static blocks in the class will be executed as the class is loaded, as long as the class is loaded It will be executed, and only initialize once, mainly used to initialize the class.

Here is an example worthy of reference

public class Person {

    public Person(int id) {
        System.out.println("person(" + id + ")");
    }
    public static void main(String[] args) {
        Build b = new Build();
        System.out.println("------分割线------");
        Build b2=new Build();
    }
}

class Build {

    /*静态块*/
    static{
        System.out.println("this is static block!");
    }
    /*非静态块*/
    {
        System.out.println("this is non-static block!");
    }
    Person p1 = new Person(1);//------------1-----------

    public Build() {
        System.out.println("this is build's block!");
        Person p2 = new Person(2);
    }

    Person p3 = new Person(3);

}

Output result:

This is static block!
This is non-static block!
person(1)
This is build's block!
person(2)
------分割线------
This is non-static block!
person(1)
This is build's block!
person(2)

It can be seen here that in the main function, when a class is initialized, the execution order is: static domain (including static attributes and static blocks)> non-static domain> attributes> constructor. It can be seen that the static block is loaded in the class Executed at the time, and after loading the generated .class file, define an instance object again, the static block will not be executed. The order of static properties and static blocks is determined according to the order of their definitions in the class.

It is precisely because the static block will only be executed once, so when the initialization operation needs to be performed once, it is placed in the static code block to effectively improve the efficiency and avoid the execution every time an object is instantiated. once.

There is another example of execution order

public class Test extends Base{
    
    

    static{
        System.out.println("test static");
    }

    public Test(){
        System.out.println("test constructor");
    }

    public static void main(String[] args) {
        new Test();
    }
}

class Base{

    static{
        System.out.println("base static");
    }

    public Base(){
        System.out.println("base constructor");
    }
}

The output result is:

base static
test static
base constructor
test constructor

That is to say, the execution starts, the main method needs to be found first, but before the main method is executed, the Test needs to be loaded first. During the process of loading the Test, it is found that the Test class inherits from the Base class. Therefore, you need to turn to load the Base class first. At that time, when a static block was found, the static block was executed. After the Base class is loaded, continue to load the Test class, and then find that there is a static block in the Test class, and execute the static block. After loading the required classes, the main method is executed. When executing newTest() in the main method, it will first call the constructor of the parent class, and then call its own constructor. Therefore, the above output result appears.

static variable

Static variables are called static variables. Static variables are initialized when the class is loaded. There is only one in memory, and the JVM will only allocate memory for it once. At the same time, all instances of the class share static variables. It can be accessed directly through the class name. But it should be noted that the Java grammar stipulates that == static is not allowed to modify the local variable ==.

But the instance variable is different. It is accompanied by an instance. Every time an instance is created, an instance variable will be generated, and it will live and die with the instance.

So we generally use static variables in these two situations: sharing data between objects and easy access.

static method

The static method is called a static method, and we can call it directly by the class name. Since static methods can be accessed without depending on any object, there is no this for static methods because it does not depend on any objects. Due to this feature, non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods/variables must rely on specific objects to be called.

In "Java Programming Thought", the static method is described as follows: "The static method is the method without this. You can't call non-static methods inside the static method, and vice versa. And you can just pass it without creating any objects. The class itself calls the static method. This is actually the main purpose of the static method."

If there are errors in the above, please correct me.
Reference materials:
https://www.cnblogs.com/dolphin0520/p/3799052.html
https://blog.csdn.net/zhangerqing/article/details/8294039
https://www.cnblogs.com/chenssy/p /3413229.html

Guess you like

Origin blog.csdn.net/dypnlw/article/details/82627588