Talking about the sequence of attribute assignment in Java

First, what are attributes?
Properties, also known as member variables, are part of a class.
We all know that a class in Java can contain:

Attributes, or member variables,
constructors
, methods, or functions,
code blocks, or program segments,
inner classes

Then combining these, we have a place where the attribute can be assigned:

1): Default initialization
2): Display initialization
3): Assignment in the constructor
4): Assignment in the code block
5): After having an object, assign a value through "object.property" or "object.method"

So what is the sequence of these methods? Let’s demonstrate it with code:
First, compare the default initialization.

package day_12_02;

/**
 * @author soberw
 */
public class Student {
    
    
    //默认初始化
    int id;
    //显示初始化
    int age = 18;
    String name = "张三";


    //构造器重新赋值
    public Student() {
    
    
        name = "李四";
    }

    //代码块中赋值
    {
    
    
        name = "王五";
    }

    //提供set方法
    public void setAge(int age) {
    
    
        this.age = age;
    }
}

class StudentTest {
    
    
    public static void main(String[] args) {
    
    
        Student student = new Student();
        System.out.println("id:" + student.id);
        System.out.println("age:" + student.age);
        System.out.println("name:" + student.name);
        //通过set方法对age属性重新赋值
        student.setAge(20);
        System.out.println("age:" + student.age);
        //通过对象.属法对name属性重新赋值
        student.name = "马六";
        System.out.println("name:" + student.name);
    }


}

operation result:
insert image description here

It is not difficult to find that after the explicit assignment, its default value is no longer displayed, so ① < ②
Then, we found that after obtaining the name attribute, the display is "Li Si", and "Li Si" is what we are constructing "Zhang San" and "Wang Wu" are not displayed in the monitor, indicating that ① < ② < ③
But when we assign values ​​through "object.property" or "object.method", the original values ​​have changed , so we get ① < ② < ③ < ⑤

Then there is another question, which one should be executed first, assignment or explicit initialization in the code block?
Let's test further:

package day_12_02;

/**
 * @author soberw
 */
public class Person {
    
    
    //声明一个属性
    int id = 0;
    //代码块中赋值
    {
    
    
        id = 1;
    }
}
class PersonTest{
    
    
    public static void main(String[] args) {
    
    
        Person p = new Person();
        System.out.println(p.id);
    }
}

Running result:
insert image description here
It is found to be "1", that is, the code block is executed first, but is this really the case?
Look again, I put the declaration attribute below:

package day_12_02;

/**
 * @author soberw
 */
public class Person {
    
    

    //代码块中赋值
    {
    
    
        id = 1;
    }
    //声明一个属性
    int id = 0;
}
class PersonTest{
    
    
    public static void main(String[] args) {
    
    
        Person p = new Person();
        System.out.println(p.id);
    }
}

Running result:
insert image description here
It is found that it has become "0" again. What is going on? In fact, it is not difficult to find that their execution order is related to their order, that is, whoever comes first will execute first. So we finally conclude:

① < ② / ④ < ③ < ⑤

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/130095795