Java basis of the static keyword

static method is not this way. You can not call non-static methods inside the static method, in turn, is possible. And you can create any objects in the absence of the premise, only to call the static method through the class itself. This actually is the main purpose of static methods. static methods can be used to modify a member of the class, the class member variables, static block may additionally write the program to optimize performance. But in Java Remember: static is not allowed to modify local variables.

static method

static methods are generally known as static methods, because the static method does not rely on any object can be accessed, so for static methods, it is not this, because it is not attached to any object, since the object did not, it would not this up. And because of this feature, non-static member variables and methods can not access non-static member class in a static method, since non-static member methods / variables are dependent on the specific object must be able to be called.

public class Test {
    
    private static void staticMethod() {
        System.out.println("静态方法调用了!");
    }
    
    public static void main(String[] args) {
        Test.staticMethod();
        Test t = new Test();
        t.staticMethod();
    }
}

Results: The
static method call!
Static method call!

static variables

also known as static variables static variables, the difference between static and non-static variables are variables: Static variables are shared by all the objects, only one copy in memory, it is only if and when the initial load class will be initialized. Rather than static variables are objects have been initialized when the object is created, there are multiple copies of each object has a copy of each other.
initialization sequence static member variables are initialized defined sequence.

public class Test {
    
    private static String name = "feigege";
    
    private static void staticMethod() {
        System.out.println("静态方法调用了!"+name);
    }
    
    public static void main(String[] args) {
        Test.staticMethod();
        Test t = new Test();
    }
}

Results: The
static method call feigege!

static block

There static keyword is a more critical role for forming a static block of code to optimize the application performance. static blocks may be placed anywhere in the class, the class may have a plurality of static blocks. When the class is loaded for the first time, each of the static block may be performed in the order of static blocks, and only once. The following code block loading sequence statci inquiry.

public class Father {
    
    public Father() {
        System.out.println("父类构造函数调用了!");
    }
    
    static {
        System.out.println("父类static代码块调用了!");
    }
}

public class Child extends Father{

    public Child() {
        super();
        System.out.println("子类构造函数执行了!");
    }
    static {
        System.out.println("子类static代码块执行了!");
    }
}

public class Test {
    
    public static void main(String[] args) {
        Child c = new Child();
    }
}

Results: The
parent class static code block calls!
Subclass a static block of code!
The parent class constructor calls!
Subclass constructor executed!
Analysis:
As to why this is the result, we would not be discussed, let's think about this code specific implementation process, at the beginning of the implementation, first find the main method, because the main entrance is to the program, but before executing the main method must be loaded Child class, discovered Test class inherits from the Father class when loading the Child class, so will turn to first class load Father, when loading the Father and the like, found static blocks, they executed a static block. After Father class loading is complete, continue to load the Child class, and then found Test class also has a static block static block is executed. After completion of the required load class, began to execute the main method. In the main method executed when new Child () will first call the parent class constructor, and then call their constructor. Therefore, there appeared above the output results.

Classic face questions

public class Test {
    
    Person person = new Person("Test");
    
    static{
        System.out.println("Test static");
    }
     
    public Test() {
        System.out.println("Test constructor");
    }
    // 执行入口
    public static void main(String[] args) {
        new MyClass();
    }
}
 
class Person{
    static{
        System.out.println("person static");
    }
    public Person(String str) {
        System.out.println("person "+str);
    }
}
 
 
class MyClass extends Test {
    
    Person person = new Person("MyClass");
    
    static{
        System.out.println("myclass static");
    }
     
    public MyClass() {
        System.out.println("myclass constructor");
    }
}

Results:
Test static
MyClass static
Person static
Person Test
Test constructor
Person MyClass
MyClass constructor
Analysis:
First Test classes loaded, it will run in the Test class static block. Then performs new new MyClass (), and MyClass class has not been loaded, it is necessary to load the class MyClass. When you load MyClass class, we found MyClass class inherits from Test class, but due to the Test class is already loaded, so only need to load MyClass class, it will perform the static block MyClass class. After completion of loading, the object can be generated by the constructor. And when generating an object, you must first initialize the member variables of the parent class, and therefore the implementation of Person person Test in = new Person (), and Person class has not been loaded before, and therefore will first load the Person class and implement the Person class the static block, then performs parent class constructor, the initialization is completed parent class, then to initialize itself, and therefore then performed in Person person MyClass = new Person (), the last executed MyClass constructor.

to sum up

When the class is loaded, performs static block.
When an object is initialized, initialize member variables, and then execute the constructor.

Guess you like

Origin www.cnblogs.com/feiqiangsheng/p/11109515.html