Parsing the usage analysis of This in Java

  • 2020-04-01 02:03:36
  • OfStack

When using the class name to define a variable, the definition should only be a reference, the outside can access the properties and methods in the class through this reference, so there should be a reference in the class to access its own properties and methods? Well, JAVA provides a nice thing called this object, which can refer to the class's properties and methods in a class. Here's a simple example:

public class ThisDemo {  
    String name="Mick";
    public void print(String name){
        System.out.println(" Properties in a class  name="+this.name);
        System.out.println(" The property of a local argument ="+name);
    }   
    public static void main(String[] args) {
        ThisDemo tt=new ThisDemo();
        tt.print("Orson");
    }
}

Thing in Java has a classic example of returning a reference to a class itself, which returns itself as an object through the keyword "this" and then does multiple operations in a single statement.

public class ThisDemo {  
    int number;
    ThisDemo increment(){
         number++;
         return this;
    }  
  private void print(){
         System.out.println("number="+number);
    }
    public static void main(String[] args) {
        ThisDemo tt=new ThisDemo();
         tt.increment().increment().increment().print();
    }
}

That should also define two constructors in one class, in one constructor to call the other constructor with this reference, and that should be implemented, and how useful would this implementation mechanism be in actually doing application development? Paste the following code:

public class ThisDemo {  
    String name;
    int age;
    public ThisDemo (){ 
        this.age=21;
   }     
    public ThisDemo(String name,int age){
        this();
        this.name="Mick";
    }     
  private void print(){
         System.out.println(" The final name ="+this.name);
         System.out.println(" Final age ="+this.age);
    }
    public static void main(String[] args) {
       ThisDemo tt=new ThisDemo("",0); //Arbitrary parameters passed in
       tt.print();
    }
}

If you look at the code above, it seems to make sense, though it's short, to assign name in a constructor with arguments and to assign age in a constructor with no arguments. But I personally think it is a little problem, instantiate a class should ThisDemo allocate memory for the object first, first calls the constructor ThisDemo (String name, int age), execute to the first line, call ThisDemo () constructor, that is to say, there should be two memory space, and one is ThisDemo (String name, int age) to perform to a point of space and the other is a ThisDemo () space, Why is it that you end up printing two properties in an object, name and age na, that are instantiated? Please comment!
To summarize:
1) this keyword is a reference to itself within the class, which can facilitate methods in the class to access their own properties;
2) you can return a reference to the class itself of the object, and you can call another constructor from one constructor (there is a problem here)

Related articles: