Java Learning - Use of this

Keywords: Attribute

1 this Calls a method in the class as an object of the current class

Example:

public class Student {
    private String name = "Zhang San";
    private Integer age = 21;

    public void prtName(){
        System.out.println("name = " + name);
    }

    public void prtAge(){
        System.out.println("age = " + age);
    }

    public void prtNameAge1(){
        this.prtName();
        this.prtAge();
    }

    public void prtNameAge2(){
        prtName();
        prtAge();
    }
}
public class ThisDemo {
    public static void main(String[] args){
        Student student = new Student();
        student.prtNameAge1();
        student.prtNameAge2();
    }
}

Run result:


In the case above, this in the prtNameAge1() method in the Student class represents ThisDemo The student object in the class.The same output as the prtNameAge1() and prtNameAge2() methods shows that calling other methods in a class method does not necessarily require this, it can be called directly using the method name, so it can be omitted from development.

2 this use in construction methods (constructors)

In constructors, this is a common use to distinguish between attribute names and method parameter names in order to avoid confusion.

Example:

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

3 Call the constructor in the constructor to reduce duplicate code

Example:

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public Student(Integer age) {
        this.age = age;
    }

    public Student(String name, Integer age) {
        this(name);
        this.age = age;
    }
}

this(name) in the example calls the Student(String name) construction method in the class to complete the construction of the name attribute, reducing code redundancy, but a constructor value allows calling a construction method, and the example calls Student(String) Name method, you cannot call the Student(Integer age) method with this;

4 this as the return value of the method returns the object that invoked the method, allowing multiple operations on the object.

Example:

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public Student(String name, Integer age) {
        this(name);
        this.age = age;
    }

    public Student prtName(){
        System.out.println("name = " + name);
        return this;
    }

    public Student prtAge(){
        System.out.println("age = " + age);
        return this;
    }

    public void prtNameAge(){
        prtName();
        prtAge();
    }
}
public class ThisDemo {
    public static void main(String[] args){
        Student student = new Student("Zeus",800);
        student.prtName().prtAge().prtNameAge();
    }
}

Run result:


The prtName() method prtAge() in the example returns the student object again so that it can be manipulated multiple times using student.prtName().prtAge().prtNameAge().

Posted by darkwolf on Tue, 02 Jul 2019 11:25:09 -0700