Java's usage of this

Source: Internet
Author: User

When defining a variable with a class name, the definition should be just a reference, which can be used to access the properties and methods inside the class, which is enough to have a reference to access their properties and methods. Oh, Java provides a good thing, is the this object, it can be in the class to reference the properties and methods of the class. Let's start with a simple example:

public class Thisdemo {      String name= "Mick";    public void print (String name) {        System.out.println ("attribute in class Name=" +this.name);        System.out.println ("property of local parameter =" +name);    }       public static void Main (string[] args) {        thisdemo tt=new thisdemo ();        Tt.print ("Orson");}    }

For a reference to the return class itself, "thinking in Java" has a classic example of returning itself to this object by this keyword and then implementing multiple operations in a single statement, or sticking it out.

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

That should also be defined in a class two constructors, in a constructor through this reference to invoke another constructor, which should be implemented, such implementation mechanism in the actual application development will have what kind of use? Put down the 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 ("Final name =" +this.name);         System.out.println ("Final age =" +this.age);    }    public static void Main (string[] args) {       Thisdemo tt=new Thisdemo ("", 0); Pass in the argument       tt.print ();    }}

Look at the above code, although it is very short, it seems logical to understand, in the constructor of the parameter is assigned to the name in the parameterless value of the age property. But I personally think that there is a problem, instantiating a class should first allocate memory for the object Thisdemo, first call the constructor Thisdemo (String name,int age), execute to the first row, call the Thisdemo () constructor, That means there should be two memory space, one is Thisdemo (String name,int age) execution to a point and the other is Thisdemo () execution space, Why did the final printout result in an instantiation of two properties in an object name and age? Please enlighten me!

To summarize:

1) The This keyword is a reference to itself within the class, and it is convenient for the method in the class to access its own properties;

2) You can return a reference to the object's own class, and you can call another constructor in one of the constructors (there is a problem in it)

Java's usage of this

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.