The This keyword in Java

Source: Internet
Author: User

The opening of the garden has been a while, but in addition to the first day vowed to send two essays, then no later. Now think about, one does not know what to write, and secondly lazy too strong, too procrastination. The reason why I think of the garden today is that I have solved a very important problem today (my feeling), and I have not found the answer that is easy to understand, title!

"This", many of the online posts said too professional, read is also confused. Today's code is clear.

First, look at the following code:

 Public class Test () {  publicint  A;  //Output statement THIS.A refers to this a     Public void Test () {  int//Does not add this call to this a System.out.println (a);
System.out.println (THIS.A);}}

look at the note (the note is added behind me, so look at the irregular), it is so simple. If you don't see it, I'll say a few more words.

1, there are two variables in this code are "a", and one is a member variable , and the other is a local variable . There is a test () method with local variables A and two output statements !

2, if there is no this, the program defaults to call a in the test () method, to call the test () class in the member variable of a is required to use this, that is, THIS.A;

3, this keyword can only appear in the method !

4. This keyword can only be used to access member variables ! If the member variable is not, an error will be provided.

The first time to write technical stickers, you are welcome to ask more opinions

The This keyword 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.