??????????????????javap???????????????????????????InnerClass????
16:03$javap-cAnotherOuterClass$InnerClass
Compiledfrom"AnotherOuterClass.java"
classAnotherOuterClass$InnerClassextendsjava.lang.Object{
finalAnotherOuterClassthis$0;
AnotherOuterClass$InnerClass(AnotherOuterClass);
Code:
0:aload_0
1:aload_1
2:putfield#12;//Fieldthis$0:LAnotherOuterClass;
5:aload_0
6:invokespecial#14;//Methodjava/lang/Object."<init>":()V
9:aload_0
10:bipush10
12:putfield#17;//Fieldx:I
15:return
staticintaccess$0(AnotherOuterClass$InnerClass);
Code:
0:aload_0
1:getfield#17;//Fieldx:I
4:ireturn
}
?????????????????????????????????????????????????access$0????????x?????
????AnotherOuterClass.class?????????
16:08 $ javap -c AnotherOuterClass
Compiled from "AnotherOuterClass.java"
public class AnotherOuterClass extends java.lang.Object{
public AnotherOuterClass();
Code:
0:  aload_0
1:  invokespecial    #8; //Method java/lang/Object."<init>":()V
4:  return
public static void main(java.lang.String[]);
Code:
0:  new  #16; //class AnotherOuterClass$InnerClass
3:  dup
4:  new  #1; //class AnotherOuterClass
7:  dup
8:  invokespecial    #18; //Method "<init>":()V
11: dup
12: invokevirtual    #19; //Method java/lang/Object.getClass:()Ljava/lang/Class;
15: pop
16: invokespecial    #23; //Method AnotherOuterClass$InnerClass."<init>":(LAnotherOuterClass;)V
19: astore_1
20: getstatic    #26; //Field java/lang/System.out:Ljava/io/PrintStream;
23: new  #32; //class java/lang/StringBuilder
26: dup
27: ldc  #34; //String InnerClass Filed =
29: invokespecial    #36; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
32: aload_1
33: invokestatic #39; //Method AnotherOuterClass$InnerClass.access$0:(LAnotherOuterClass$InnerClass;)I
36: invokevirtual    #43; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
39: invokevirtual    #47; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
42: invokevirtual    #51; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
45: return
}
??????????????????????????????????????????x?????
????33:   invokestatic #39; //Method AnotherOuterClass$InnerClass.access$0:(LAnotherOuterClass$InnerClass;)I
?????????????
????????java?????? ????????仰
????if the member or constructor is declared private?? then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
????????? ???????????????????????趨??????????η????????????????????????????
????????????????г????????????
?????????????????????????????????????г??????????????????????????????“????????”???????????????????????????????????
????????mRunnable??????????Runnable???????????????????????????????????????????Runanble?????x????????????mRunnable.x???????????
public class PrivateToOuter {
Runnable mRunnable = new Runnable(){
private int x=10;
@Override
public void run() {
System.out.println(x);
}
};
public static void main(String[] args){
PrivateToOuter p = new PrivateToOuter();
//System.out.println("anonymous class private filed= "+ p.mRunnable.x); //not allowed
p.mRunnable.run(); // allowed
}
}
?????????
??????????У?private???????????Ч??????????????е?????????????????????????????е??????
????Java?????????????ж??????????C++??????????C++???????