Boring JavaEE from entry to abandonment (5) the essence of this_4 steps in the process of object creation_implicit parameters & static

table of Contents

One.this keyword

1. The process of object creation and the nature of this

2. The most common usage of this:

Two.static keyword

Three. Variable classification


One.this keyword

1. The process of object creation and the nature of this

The construction method is an important way to create a Java object. When the constructor is called through the new keyword, the constructor does return an object of this class, but this object is not completely created by the constructor.

Creating an object is divided into the following four steps:
1. Allocate the object space and initialize the object member variables to 0 or empty
2. Perform explicit initialization of the attribute value (int id)
3. Perform the construction method
4. Return the address of the object to
The essence of the related variable this is "the address of the created object"! Because the object has been created before the constructor is called.
Therefore, you can also use this to represent the "current object" in the construction method.

2. The most common usage of this:

Where there is ambiguity in the program, this should be used to indicate the current object; in ordinary methods, this always refers to the object that called the method. In the construction method, this always points to the object being initialized.
Use this keyword to call the overloaded construction method to avoid the same initialization code. But it can only be used in the construction method, and must be in the first sentence of the construction method.
this cannot be used in static methods.

public class User {
    int id;//用户名
    String name;//账户名
    String pwd;//密码
    public User(int id,String name){
        this.id=id; //用this来避免歧义
        this.name=name;
    }

    public User(int id, String name, String pwd) {
        //this可以调用其他构造器,且必须位于第一位
        this(id,name); //相当于调用了上面的构造函数
        this.pwd = pwd;
    }

    public void login(){
        System.out.println("登陆了:"+this.name); //快捷键sout
    }

    public static void main(String[] args) { //快捷键psvm
        User u1=new User(100,"hh");
        u1.login();
    }
}

Two.static keyword

In a class , member variables declared with static are static member variables , also called class variables. The life cycle of a class variable is the same as that of a class, and it is valid during the execution of the entire application.

It has the following characteristics: it
is a public variable of the class, belongs to the class, is shared by all instances of the class, and is explicitly initialized when the class is loaded.
For all objects of this class, there is only one static member variable. Shared by all objects of this class!!
Generally called by "class name class attribute/method". (You can also access static members through object references or class names (without instantiation).)
Non-static members cannot be accessed directly in the static method.

public class User {
    int id;//用户名
    String name;//账户名
    String pwd;//密码
    static String affection="爱情好苦";

    public User(int id,String name){
        this.id=id; //用this来避免歧义
        this.name=name;
    }

    public User(int id, String name, String pwd) {
        //this可以调用其他构造器,且必须位于第一位
        this(id,name); //相当于调用了上面的构造函数
        this.pwd = pwd;
    }

    public void login(){
        System.out.println("登陆了:"+this.name); //快捷键sout
    }

    public static void printCompany(){
        //login();调用非静态成员,编译就会报错
        System.out.println(affection);
    }

    public static void main(String[] args) { //快捷键psvm
        User u1=new User(100,"hh");
        User.printCompany();
        User.affection="我选择放弃";
        User.printCompany();
    }
    
    static{
        System.out.println("类的静态初始化");
        affection="test";
    }
}

Output:

类的静态初始化
test
我选择放弃

Three. Variable classification

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115316645