Use of the keyword this in Java

Source: Internet
Author: User

The This keyword is used in the team code to see the methods in the current class. After testing, it is found that the use of the This keyword is optional in this case. Therefore, the use of this in Java is summarized as follows:

 PackageTestthis; Public classTestthis {intFlag = 0;  Public Static voidMain (string[] args) {test test=NewTest ();        Test.main (); Testthis TT=NewTestthis (); Tt.say (); //cannot use This.say ();    }     Public voidsay () {MyTest Mt=NewMyTest ();        Mt.main (); inti = This. Flag; intK =Flag; }}classTest { Public voidMain () {say1 ();  This. Say1 ();        Say2 ();  This. Say2 ();        Say3 ();  This. Say3 ();        Say4 ();  This. Say4 (); }     Public voidsay1 () {System.out.println ("111111111111111"); }    protected voidSay2 () {System.out.println ("222222222222222"); }    voidsay3 () {System.out.println ("333333333333333"); }    Private voidSay4 () {System.out.println ("444444444444444"); }}classMyTestextendsTest {@Override Public voidMain () { This. Say1 (); }}
case where the This keyword is not recommended in Java

For both cases, you must use the keyword this. The methods in the constructor and inner classes call the external class, and the demo is as follows:

 Public classTest {Private Final intNumber ;  PublicTest (intNumber ) {         This. Number = number;//Output 5//Number = number; output 0    }     Public Static voidMain (string[] args) {Test ts=NewTest (5);    System.out.println (Ts.number); }}

The example code above shows how this is used in the construction method, and if not used, an error occurs. In addition, we can look at the source code of the JDK, such as many of the constructor methods in the string class, all using the This keyword. In addition, I feel that in this many cases it is just a function of identity, such as distinguishing between this.name one's super.name own and one is the parent class. Just for the code to be readable.

 Public classA {inti = 1;  PublicA () {//thread is an anonymous class object that uses the Run method of the external class in its Run method//because the function has the same name, the direct call is not//Workaround: The class name of the external class plus the this to indicate the method to invoke the external class run//The class name can be used when there is an event listener or other method inside the class to invoke a reference to the class, with this error. This, it's OK.Thread thread =NewThread () {@Override Public voidrun () { for (;;) {A. This. Run (); Try{sleep (1000); } Catch(Interruptedexception IE) {}}}        };    Thread.Start (); }     Public voidrun () {System.out.println ("I =" +i); I++; }     Public Static voidMain (string[] args)throwsException {NewA (); }}

The above shows the use of the keyword this in an anonymous inner class, when the method in the inner class and the method in the outer class have the same name, if you want to use the same name method in the outer class in the inner class, use the external class. This method class to display the use of the same name method in the external class.

Use of the keyword this 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.