Access Modifiers for Inner Classes
suggest changeA full explanation of Access Modifiers in Java can be found here. But how do they interact with Inner classes?
public, as usual, gives unrestricted access to any scope able to access the type.
public class OuterClass {
public class InnerClass {
public int x = 5;
}
public InnerClass createInner() {
return new InnerClass();
}
}
public class SomeOtherClass {
public static void main(String[] args) {
int x = new OuterClass().createInner().x; //Direct field access is legal
}
}
both protected and the default modifier (of nothing) behave as expected as well, the same as they do for non-nested classes.
private, interestingly enough, does not restrict access to the nested class that declares the member. It restricts access to the body of the top-level class that encloses the declaration, not to every type in the same .java file. The outer class can therefore use the inner class’s fields and methods even when they are marked private.
public class OuterClass {
public class InnerClass {
private int x;
private void anInnerMethod() {}
}
public InnerClass aMethod() {
InnerClass a = new InnerClass();
a.x = 5; //Legal
a.anInnerMethod(); //Legal
return a;
}
}
The Inner Class itself can have a visibility other than public. By marking it private or another restricted access modifier, other (external) classes will not be allowed to import and assign the type. They can still get references to objects of that type, however.
public class OuterClass {
private class InnerClass{}
public InnerClass makeInnerClass() {
return new InnerClass();
}
}
public class AnotherClass {
public static void main(String[] args) {
OuterClass o = new OuterClass();
InnerClass x = o.makeInnerClass(); //Illegal, can't find type
OuterClass.InnerClass x = o.makeInnerClass(); //Illegal, InnerClass has visibility private
Object x = o.makeInnerClass(); //Legal
}
}