Today when writing a small program appears no enclosing instance of type Hidden is accessible. Must qualify the allocation with an enclosing instance of type Hidden (e.g. X.new A () where X was an instance of Hidden in). The wrong The mistake, looked for a long time, finally solved.
My program is:
public class Hidden {
Class father{
/*public void Str () {
System.out.println ("str! of the output parent class");
}*/
String str= "STR member of the parent class";
}
Class Son extends father{
/*public void Str () {
System.out.println ("str! of the output subclass");
}*/
String str= "STR member of Subclass";
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Father father=new Father ();
Son son=new son ();
System.out.println ("Method of Output Parent class:");
System.out.println (FATHER.STR);
System.out.println ("Method of output subclass:");
System.out.println (SON.STR);
}
}
Runtime hint: No enclosing instance of type Hidden is accessible. Must qualify the allocation with an enclosing instance of type Hidden (e.g. X.new A () where X was an instance of Hidden.
Modified several times, but still can not solve. Finally found an article on the internet, solved.
The father and son classes are internal classes, and only static classes can be instantiated. So add the father and the son class to the front of the static.
Or:
public class Hidden {
Class father{
/*public void Str () {
System.out.println ("str! of the output parent class");
}*/
String str= "STR member of the parent class";
}
Class Son extends father{
/*public void Str () {
System.out.println ("str! of the output subclass");
}*/
String str= "STR member of Subclass";
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Hidden hidden=new Hidden ();
Father father=hidden.new Father ();
Son son=hidden.new son ();
System.out.println ("Method of Output Parent class:");
System.out.println (FATHER.STR);
System.out.println ("Method of output subclass:");
System.out.println (SON.STR);
}
}
Original link: http://www.tuicool.com/articles/RFrANzr
Java error No enclosing instance of type Hidden is accessible