This keyword parsing in Java

Source: Internet
Author: User

Because this keyword is often seen in Java programs, the author simply summed up its usage, and everyone to learn from each other. In general this is used in several places:

(1) When the local variable and the member variable have the same name, you need to use this to distinguish

such as: public class text (int number) {

This.number=number; Write number=number directly; get 0, because it doesn't make sense to assign yourself to yourself.

System.out.println ("number=" +this.number);

}

(2) This () represents the invocation of another constructor, which calls that function, depending on the type and number of arguments given when this () is called.

Note: the A.this () call can only appear in the first row of the constructor;

B. Constructors cannot be called within any function other than the constructor;

C. Only one constructor can be called within a constructor;

Readers can think about why these considerations occur.

(3) This represents a reference to the current object

such as: public class Thistest () {

pricate int j=0;

for (int i=0;i<3;i++) {

this.j=j++;

}

return this;

}

(4) This cannot be used in the static method. Because the this keyword points to the current object, and the static adornment method belongs to the class, the caller of the class may be a class rather than an object, causing this to fail to point to the appropriate object.

I hope that the author's summary can bring some help to everyone, because the author's level of limited also hope that the great God all my mistakes to point out. For everyone's problems can also be at the end of the message, we can exchange learning together.

This keyword parsing in Java

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.