http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class/70358
http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class/70358
But can any body tell me what s the difference between Inner.redo1() and Inner.redo2()?
Or Inner.print1() and Inner.print2() are the same?
public class Outer {
private String str = "outer";
public void print() {
System.out.println("a");
}
public class Inner {
public void redo1() {
print();
}
public void redo2() {
Outer.this.print();
}
}
}
PS: In java.util.ArrayList.Itr#remove
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
Why does it use ArrayList.this.remove(lastRet); but not remove(lastRet); ?
But can any body tell me what s the difference between Inner.redo1() and Inner.redo2()?
Inner.redo1() and Inner.redo2() are indeed the same. There is no semantic or functional difference. redo2 is more explicit. That is all.
Let s compare each method s bytecode:
λ Desktop javap -c Outer.Inner
Compiled from "Outer.java"
public class Outer$Inner extends java.lang.Object{
final Outer this$0;
public Outer$Inner(Outer);
Code:
0: aload_0
1: aload_1
2: putfield #1; //Field this$0:LOuter;
5: aload_0
6: invokespecial #2; //Method java/lang/Object."<init>":()V
9: return
public void redo1();
Code:
0: aload_0
1: getfield #1; //Field this$0:LOuter;
4: invokevirtual #3; //Method Outer.print:()V
7: return
public void redo2();
Code:
0: aload_0
1: getfield #1; //Field this$0:LOuter;
4: invokevirtual #3; //Method Outer.print:()V
7: return
}
Same bytecode, same end result, same everything.
Why does it use ArrayList.this.remove(lastRet); but not remove(lastRet); ?
To disambiguate which class remove() method should actually be called. It is actually necessary in this case! Without explicitly specifying ArrayList.this.remove(lastRet), the code does not compile:
λ Desktop javac java/util/ArrayList.java
java/util/ArrayList.java:810: remove() in java.util.ArrayList<E>.Itr cannot be applied to (int)
remove(lastRet);
^
1 error
http://stackoverflow.com/questions/16409210/accessing-outer-class-from-inner-class-in-java