I have read a sentence here like below
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the super class.
Now I have a code like below
class Locker {
private String secret = "This is my secret";
public class Util { }
}
class StealSecret extends Locker.Util { }
But I am getting an compile time error like below
No enclosing instance of type Locker is available due to some intermediate constructor invocation
I am unable to understand what exactly is causing the compile time error. Is it that what I read in the link is wrong ?
Please give me an example explaining the way to access the private members of enclosing class by extending the inner class
Util
class as static. You get this error because the inner class can only exist when an instance of outer class exists. Since you did not instantiate the outer class, inner class won't be available. But thats gonna bring you to whole another story with your testing.... – Jaskaranbir Singh