Java error No enclosing instance of type Hidden is accessible

Source: Internet
Author: User
Tags stub

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.