Parse the usage analysis of this in Java _java

Source: Internet
Author: User
When defining a variable with the class name, the definition should be just a reference, which can be used to access the properties and methods inside of the class, which is enough or should have a reference to access its own properties and methods na? Oh Java provides a good thing, the This object, which can refer to the class's properties and methods in the class。 Let's start with a simple example:
Copy Code code as follows:

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

With reference to the return class itself, Thing in Java has a classic example of returning the object through this keyword and then implementing multiple operations in a single statement, or sticking it out.
Copy Code code as follows:

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 be defined in a class of two constructors, in a constructor through this reference to invoke another constructor, which should be implemented, this implementation mechanism in the actual application development when there will be what kind of use? Post-Write code:
Copy Code code as follows:

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 ("the ultimate age =" +this.age);
}
public static void Main (string[] args) {
Thisdemo tt=new Thisdemo ("", 0); A random pass in the parameters
Tt.print ();
}
}

Looking at the above code is very short, it seems to be logical to understand, in a constructor with parameters assignment name is assigned the age attribute among the parameters. But I personally think there's a problem, instantiating a class should allocate memory for the object Thisdemo first, call the constructor Thisdemo (String Name,int age), and when executed to the first row, call the Thisdemo () constructor, That is, there should be two memory space, one is Thisdemo (String name,int age) to a point of space and the other is Thisdemo () execution space, Why did the final printout result in the instantiation of two attribute name and age na in an object? Daniel, please enlighten me!
To sum up:
1) This keyword is a reference to itself within the class, and it is convenient for the methods in the class to access their own properties;
2 You can return a reference to the object's own class, and you can also invoke another constructor in one of the constructors (this one has a problem)

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.