The This keyword in Java

Source: Internet
Author: User

this: appears to be used to distinguish between local variables and member variables with the same name.

This is therepresentative of this kind of object, in the end is representing which one?

This represents a reference to the object to which the function is located.

Simply put, which object is the one that is calling this function, this represents which object.

Class children{

private String name;

private int age;

Children (String name) {

This.name=name;

}

Public boolean compare (person p) {

return this.age==p.age;

}

public static void Main (string[] str) {

Children c1=new Children ("Allen");//this representative C1

Children c2=new Children ("Rose");//this representative C2

C1.compare (C2);

}

}

This applies: When you define a function in a class, it is used inside the function to use this to represent the object when the function is called.

This type of object is used internally for this kind of function .

The This keyword is called between constructors:

Class children{

private String name;

private int age;

Children (String name) {

This.name=name;

}

Children (String Name,int age) {

This (name);

This.age=age;

}

}

This statement: used to call each other between constructors. You can only define the first row in the constructor, because initialization is performed first.

The This keyword in Java

Related Article

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.