java class name this

The syntax “Classname.this” is for inner classes. If you want to refer to the enclosing instance of type “Outerclass” then you do it as “Outerclass.this”.

public class Siht {
    class NestedSiht {
        void demoThis() {
            System.err.println("this' is an instance of: " + 
                            this.getClass().getName());
            System.err.println("Siht.this' is an instance of: " +
                            Siht.this.getClass().getName());
        }
    }


void demoThis() {
    new java.lang.Object() {
        void demoThis() {
            System.err.println("`this' is an instance of: " + 
                            this.getClass().getName());
            System.err.println("`Siht.this' is an instance of: " +
                            Siht.this.getClass().getName());
        }
    }.demoThis();
    new NestedSiht().demoThis();
}

public static void main(String [] args) {
    new Siht().demoThis();
}


}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s