I have a class MyClass
and an inner class MyNestedClass
like this:
public class MyClass {
...
public class MyNestedClass {
You can make the inner class package private which means that it will only be accessible from other classes in exactly the same package. This is also done quite frequently for hidden classes inside the standard JDK packages like java.lang
or java.util
.
in pkg/MyClass.java
public class MyClass {
...
}
in pkg/MyHiddenClass.java
class MyHiddenClass {
final MyClass outer;
MyHiddenClass( MyClass outer )
{
this.outer = outer;
}
...
}
Now when you want to access methods or variables of the outer class you need to prefix them with outer.
but you get essentially the same functionality as before when the reference to the outer instance was synthetically created by the compiler.