I have two compilation units:
public class OuterClass{
private static class InnerClass{
public String test(){
return "testing123";
}
}
public static void main( String[] args ){
new CallingClass().test( new InnerClass() );
}
}
public class CallingClass{
public void test( Object o ){
try{
Method m = o.getClass().getMethod( "test" );
Object response = m.invoke( o );
System.out.println( "response: " + response );
}
catch( Exception e ){
e.printStackTrace();
}
}
}
If they are in the same package, everything works and "response: testing123" is printed. If they are in separate packages, IllegalAccessException is thrown.
As I understand, exception is thrown because CallingClass cannot invoke private InnerClass methods. But what I do not understand is why is it allowed in the same package? InnerClass is not package protected. Private should not be visible outside OuterClass even if it is in the same package. Do I understand something wrong?
Private access modifier is stronger than package one (i.e. not having an access modifier at all in Java). Hence private elements of class - be it fields, methods or inner classes - are accessible only within that class.
The javap signature for an inner class:
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: