Use of Classname.this and this in java/android

Source: Internet
Author: User

If you use this in an inner class to refer to an instance of the inner class, and if Outerclassname.this is the instance of the class outside it


Classname.this This usage is used in nested class (inner class), when inner class (inner class) must be used to outer class (external Class) of this instance (instance), Just use Outerclassname.this.


Places that are commonly used in Android such as:

Button button = (button) Findviewbyid (r.id.ticket_details_sell_ticket); Button.setonclicklistener (new  Onclicklistener () {    @Override public    void OnClick (View v) {        //It is wrong to use only ' this ', because it would        //reference the just created Onclicklistener object        Intent login = new Intent (Classname.this, Login.class) ;        Startactivityforresult (login, login_request);    });


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.