How this keyword works in inner class ?

Within an inner class code, the this reference refers to the instance of the inner
class, as you'd probably expect, since this always refers to the currently executing
object.
Example:
class MyOuter {
private int x = 7;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Inner class ref is " + this);
System.out.println("Outer class ref is " + MyOuter.this);
}
}
public static void main (String[] args) {
MyOuter.MyInner inner = new MyOuter().new MyInner();
inner.seeOuter();
}}

the output is something like this:
Outer x is 7
Inner class ref is MyOuter$MyInner@113708
Outer class ref is MyOuter@33f1d7

So the rules for an inner class referencing itself or the outer instance are as follows:
■ To reference the inner class instance itself, from within the inner class code,
use this.
■ To reference the "outer this" (the outer class instance) from within the inner
class code, use NameOfOuterClass.this (example, MyOuter.this).

Modifiers Applied to Inner Classes
A regular inner class is a member of the outer class just as instance variables and methods are, so the following modifiers can be applied to an inner class:
■ final
■ abstract
■ public
■ private
■ protected
■ static—but static turns it into a static nested class not an inner class
■ strictfp

Key points:
❑ A "regular" inner class is declared inside the curly braces of another class, but
outside any method or other code block.
❑ An inner class is a full-fledged member of the enclosing (outer) class, so it
can be marked with an access modifier as well as the abstract or final
modifiers. (Never both abstract and final together— remember that
abstract must be subclassed, whereas final cannot be subclassed).
❑ An inner class instance shares a special relationship with an instance of the
enclosing class. This relationship gives the inner class access to all of the
outer class's members, including those marked private.
❑ To instantiate an inner class, you must have a reference to an instance of the
outer class.
❑ From code within the enclosing class, you can instantiate the inner class
using only the name of the inner class, as follows:
MyInner mi = new MyInner();
❑ From code outside the enclosing class's instance methods, you can
instantiate the inner class only by using both the inner and outer class names,
and a reference to the outer class as follows:
MyOuter mo = new MyOuter();
MyOuter.MyInner inner = mo.new MyInner();
❑ From code within the inner class, the keyword this holds a reference to
the inner class instance. To reference the outer this (in other words, the
instance of the outer class that this inner instance is tied to) precede the
keyword this with the outer class name as follows: MyOuter.this;TION OBJECTIVE

3 comments:

  1. I read your post and it was amazing , KEEP IT, great job.

    PIC Bonus Singapore

    ReplyDelete
  2. perfect explanation about java programming .A regular inner class is declared inside the curly braces of another class, but
    outside any method or other code block.its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete