The use of this keyword in Java

1, Represents an attribute in a class

1. this is not used

 
class Person{        // Define the Person class
    private String name ;        // full name
    private int age ;            // Age
    public Person(String name,int age){    // Assignment by construction method
        name = name ;
        age = age ;
    }
    public String getInfo(){    // Methods of obtaining information
        return "full name:" + name + ",Age:" + age ;
    }
};
public class ThisDemo01{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",33) ;    // Call to construct an instantiated object
        System.out.println(per1.getInfo()) ;    // Get information
    }
};
Operation results:
full name: null,Age: 0;
 

It can be concluded that the content is not correctly assigned to the attribute at this time;

If there is a pen around you and a pen in the distance, you will definitely take the pen nearby. This is the same in the construction method.

Therefore, the name and age of the operation at this time are the name and age defined in the construction method It has nothing to do with the properties in the class.

At this point, in order to specify which attribute is in the class, you need to add this Class.

 
class Person{        // Define the Person class
    private String name ;        // full name
    private int age ;            // Age
    public Person(String name,int age){    // Assignment by construction method
        this.name = name ; // Assign a value to the name attribute in the class
        this.age = age ;// Assign a value to the age attribute in the class
    }
    public String getInfo(){    // Methods of obtaining information
        return "full name:" + name + ",Age:" + age ;
    }
};
public class ThisDemo02{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",33) ;    // Call to construct an instantiated object
        System.out.println(per1.getInfo()) ;    // Get information
    }
};
Operation results:
Name: Zhang San, age: 33
 

2, this calls the constructor

If there are multiple constructors in the class, you can also call each other with the keyword this.

Suppose that there are multiple construction methods in a class, but no matter how many construction methods, it is necessary to create a "new object instantiation". At this time, there are two methods.

According to the original method:

 
class Person{        // Define the Person class
    private String name ;        // full name
    private int age ;            // Age
    public Person(){    // Nonparametric structure
        System.out.println("New object instantiation") ;
    }
    public Person(String name){
        System.out.println("New object instantiation") ;
        this.name = name ;
    }
    public Person(String name,int age){    // Assignment by construction method
        System.out.println("New object instantiation") ;
        this.name = name ; // Assign a value to the name attribute in the class
        this.age = age ;// Assign a value to the age attribute in the class
    }
    public String getInfo(){    // Methods of obtaining information
        return "full name:" + name + ",Age:" + age ;
    }
};
public class ThisDemo03{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",33) ;    // Call to construct an instantiated object
        System.out.println(per1.getInfo()) ;    // Get information
    }
};
 

Because the above red part is repeated, it is only one line and can't be felt. However, if there are many lines of code, the defects will appear immediately.

At this point, it is best to call each other between constructors.

Use the form of: this (several parameters).

 
package methoud;
class Person{        // Define the Person class
    private String name ;        // full name
    private int age ;            // Age
    public Person(){    // Nonparametric structure        
        System.out.println("New object instantiation") ;
    }
    public Person(String name){
        this() ;// Call the parameterless construction method in this class
        this.name = name ;
    }
    public Person(String name,int age){    // Assignment by construction method
        this(name) ;// Call the constructor with one parameter
        this.age = age ;// Assign a value to the age attribute in the class
    }
    public String getInfo(){    // Methods of obtaining information
        return "full name:" + name + ",Age:" + age ;
    }
};
public class ThisDemo06{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",33) ;    // Call to construct an instantiated object
        System.out.println(per1.getInfo()) ;    // Get information
    }
};
Operation results:
New object instantiation
Name: Zhang San, age: 33
 

Note:

When using this keyword to call other keywords, there are the following restrictions:

1) The statement of this() calling other construction methods can only be placed on the first line of the construction method (not in other ordinary methods);

2) When using this to call other constructor methods, at least one constructor does not need to be called by this. (it must have an end and cannot be called indefinitely. It is called circularly and recursively);

An error will occur as follows:

 

 
package methoud;
class Person{        // Define the Person class
    private String name ;        // full name
    private int age ;            // Age
    public Person(){    // Nonparametric structure
        System.out.println("New object instantiation") ;
    }
    public Person(String name){
        this.name = name ;
        this() ;// Calling this() method can only be placed on the first line of the constructor
    }
    public Person(String name,int age){    // Assignment by construction method
        this(name) ;// Call the constructor with one parameter
        this.age = age ;// Assign a value to the age attribute in the class
    }
    public String getInfo(){    // Methods of obtaining information
        this() ;// Other ordinary methods cannot call this() method
        return "full name:" + name + ",Age:" + age ;
    }
};
public class ThisDemo04{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",33) ;    // Call to construct an instantiated object
        System.out.println(per1.getInfo()) ;    // Get information
    }
};
 

 

 

 

3, Use this keyword to call the current object.

Current object: the object that is currently calling the method.

As follows, the objects per1 and per2 are printed in two ways

 
class Person{        // Define the Person class
    public String getInfo(){    // Methods of obtaining information
        System.out.println("Person class --> " + this) ; // Print this directly
        return null ; // To ensure correct syntax, null is returned
    }
};
public class ThisDemo06{
    public static void main(String args[]){
        Person per1 = new Person() ;    // Call to construct an instantiated object
        Person per2 = new Person() ;    // Call to construct an instantiated object
        System.out.println("MAIN method --> " + per1) ;    // Print objects directly
        per1.getInfo() ;    // The object that currently calls the getInfo() method is per1
        System.out.println("MAIN method --> " + per2) ;    // Print objects directly
        per2.getInfo() ;    // The object that currently calls the getInfo() method is per2
    }
};
Operation results:

MAIN method --> methoud.Person@2a139a55
Person class --> methoud.Person@2a139a55
MAIN method --> methoud.Person@15db9742
Person class --> methoud.Person@15db9742

 

It can be seen that the current object is called with this, which has the same effect as Per1 and per2 directly.

 

4, Object comparison

 

In the following example, two objects are generated. When the age and name of the objects are completely equal, the two objects are considered equal. At this time, there are two problems

1) How to compare objects:

2) In that object comparison:

String itself is a class. If you want to make equality comparison, you need to use equls(), while age is Int, and you can directly use = = to judge.

 
class Person{        // Define the Person class
    private String name ;    // full name
    private int age ;        // Age
    public Person(String name,int age){
        this.setName(name) ;
        this.setAge(age) ;
    }
    public void setName(String name){    // Set name
        this.name = name ;
    }
    public void setAge(int age){        // Set age
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age ;
    }
};
public class ThisDemo07{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",30) ;    // Declare two objects with exactly the same content
        Person per2 = new Person("Zhang San",30) ;    // Declare two objects with exactly the same content
        // Get each attribute in turn directly in the main method for comparison
        if(per1.getName().equals(per2.getName())&&per1.getAge()==per2.getAge()){
            System.out.println("Two objects are equal!") ;
        }else{
            System.out.println("Two objects are not equal!") ;
        }
    }
};
Operation results:
Two objects are equal!
 

The functions of the above code are indeed realized, but because the code is exposed, it is unsafe and error prone.

It is most appropriate to compare by yourself, so a comparison method should be added to the Person class.

The current object calls the object passed on. The current object is the object calling the method, which is represented by this,

Here means where to compare.

As follows:

class Person{        // definition Person class
    private String name ;    // full name
    private int age ;        // Age
    public Person(String name,int age){
        this.setName(name) ;
        this.setAge(age) ;
    }
    public boolean compare(Person per){
        // When calling this method, there are two objects: the current object and the incoming object
         Person p1 = this ;    // The current object represents per1
        Person p2 = per ;    // The object passed in represents per2
        if(p1==p2){    // Determine whether it is the same object and compare with the address
            return true ;
        }
        // Then judge whether each attribute is equal
        if(p1.name.equals(p2.name)&&p1.age==p2.age){
            return true ;    // Two objects are equal
        }else{
            return false ;    // Two objects are not equal
        }
    }
    public void setName(String name){    // Set name
        this.name = name ;
    }
    public void setAge(int age){        // Set age
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age ;
    }
};
public class ThisDemo08{
    public static void main(String args[]){
        Person per1 = new Person("Zhang San",30) ;    // Declare two objects with exactly the same content
        Person per2 = new Person("Zhang San",30) ;    // Declare two objects with exactly the same content
        // Get each attribute in turn directly in the main method for comparison
        if(per1.compare(per2)){
            System.out.println("Two objects are equal!") ;
        }else{
            System.out.println("Two objects are not equal!") ;
        }
    }
};
Operation results:
Two objects are equal!

 

More previous videos can be viewed on my Bili Bili: Codebond master's latest Java video tutorial in 2020

 

 

Posted by om.bitsian on Wed, 04 May 2022 08:18:01 +0300