Application of this keyword in Constructor

Source: Internet
Author: User

/**
* Created by Liu pengcheng. on 2014-6-12. Blog

Usage of this

1. Used to differentiate variables with the same name (when a member variable and a local variable have the same name)

2. It is used to call each other between constructors. This statement can only be defined in the first line of the constructor to be executed before initialization.
*/
Class this_persion {
Private String name;
Private int age;

This_persion ()
{

}
This_persion (String name)
{
This. name = name;
}
This_persion (String name, int age)
{
This (name );
This. age = age;
}
Public boolean compare (this_persion p)
{
Return this. age = p. age;
}

}

Created by Liu pengcheng. on. Blog
Class this_persionDomo
{
Public static void main (String [] args)
{
This_persion p1 = new this_persion ("Zhang San", 35 );
This_persion p2 = new this_persion ("Zhang San", 35 );
Boolean B = p1.compare (p2 );
System. out. println (B );
}
}

Created by Liu pengcheng. on. Blog

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.