Accessing private inner class in the same package

前端 未结 2 956
礼貌的吻别
礼貌的吻别 2021-02-20 07:12

I have two compilation units:

public class OuterClass{

    private static class InnerClass{

        public String test(){
            return \"testing123\";
           


        
2条回答
  •  别那么骄傲
    2021-02-20 07:34

    The javap signature for an inner class:

    class modifiers.OuterClass$InnerClass extends java.lang.Object{
        final modifiers.OuterClass this$0;
        public java.lang.String test();
    }
    

    When it comes to bytecode (i.e. runtime) there is no such thing as a private class. This is a fiction maintained by the compiler. To the reflection API, there's a package-accessible type with a public member method.

    Actual access modifiers are defined in the JVM spec:

    Flag Name      Value   Interpretation
    ACC_PUBLIC     0x0001  Declared public; may be accessed from outside its package.
    ACC_FINAL      0x0010  Declared final; no subclasses allowed.
    ACC_SUPER      0x0020  Treat superclass methods specially when invoked by the
                           invokespecial instruction.
    ACC_INTERFACE  0x0200  Is an interface, not a class.
    ACC_ABSTRACT   0x0400  Declared abstract; may not be instantiated. 
    

提交回复
热议问题