Question
When an inner class is created (NB: non static), it will receive a reference to the outer class (by its construction). What guarantees apply in this case (if any) with respect to the preservation of this link?
Long explanation (attempt to give meaning to the question)
Consider this simple code:
public class OuterInnerExample {
private int mInt;
public void startJob() {
InnerRunnable r = new InnerRunnable();
Thread t = new Thread(r);
t.start();
}
private class InnerRunnable implements Runnable {
public void run() {
int localInt = mInt;
System.out.println(localInt);
}
}
}
After compilation, if we decompile class files (I used jad), we can notice that it InnerRunnablehas a member this$0that is a reference to an object OuterInnerExample.
- InnerRunnable super() ( , this$0 null, , .. ).
InnerRunnable mInt, getter, , OuterInnerExample ( this$0). getter access$100.
, Java : http://pastebin.com/gr8GB03t.
, ( ) , :
FATAL EXCEPTION: main
java.lang.NullPointerException
at <package.name>.OuterInnerExample.access$100(<line where "class OuterInnerExample" is>
at <package.name>.OuterInnerExample$InnerRunnable.run(<line where "public void run()" is>)
, this$0 null .