How do I use the This keyword in Java? when can I use it?

Source: Internet
Author: User

In Java, this usually refers to the current object, or to a member of the current object, and you can use this for this purpose. Another use of this is to invoke another constructor of the current object.

The most common scenario is that a parameter name in your method has the same name as a member of the current object, so you need to explicitly use the This keyword to kill you to use a member, the method "this. Member name", and the one without this is the formal parameter. In addition, You can also use the "this. Method name" to refer to a method of the current object, but this is not necessary, you can directly use the method name to access the method, the compiler will know which one you want to invoke.

Use:
Distinguishing member variables from local variables

this. Variable name member variable
This. Method name (..) member method
This (..) Structure

Usage scenarios:
With parametric construction
Get/set

How do I use the This keyword in Java? when can I use it?

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.