Java错误No enclosing instance of type Hidden is accessible

今天在写一个小的程序时出现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 is an instance of Hidden).的错误,寻找了很长时间,终于解决了。

我的程序是:

public class Hidden {

class Father{
/*public void str(){
//System.out.println("输出父类的str!");
}*/
String str="父类的STR成员";
}

class Son extends Father{
/*public void str(){
System.out.println("输出子类的str!");
}*/
String str="子类的STR成员";
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Father father=new Father();
Son son=new Son();
System.out.println("输出父类的方法:");
System.out.println(father.str);
System.out.println("输出子类的方法:");
System.out.println(son.str);
}

}

运行时提示: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 is an instance of Hidden).

着了修改了好几次,但是还是解决不了。最后在网上找了一篇文章,解决了。

Father和Son类都是内部类,只有静态类才能被实例化。所以将Father和Son类前面添加static就可以了。

或者:

public class Hidden {

class Father{
/*public void str(){
//System.out.println("输出父类的str!");
}*/
String str="父类的STR成员";
}

class Son extends Father{
/*public void str(){
System.out.println("输出子类的str!");
}*/
String str="子类的STR成员";
}

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("输出父类的方法:");
System.out.println(father.str);
System.out.println("输出子类的方法:");
System.out.println(son.str);
}

}

原文链接:http://www.tuicool.com/articles/RFrANzr

时间: 2024-11-03 01:37:26

Java错误No enclosing instance of type Hidden is accessible的相关文章

java解决 No enclosing instance of type XXX is accessible的问题

有些时候我们要把Activity的一些实现类移到java类里来实现,比如把写以下两个类: 在LifeCircle这个类中: public class LifeCircle { public class Mybroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { } } } 如果在Activity这样去new: public class Setti

Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ins

Java 出现 No enclosing instance of type Test1 is accessible.(转,自己也遇到了!)

最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g.  x.new A() where x is an instance of E). E指代我写的那个内部类. 根据提示,没有可访问的内部类E的实例,必须分配一

Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题

今天在编译Java程序时遇到如下问题: No enclosing instance of type PrintListFromTailToHead is accessible. Must qualify the allocation with an enclosing instance of type PrintListFromTailToHead (e.g. x.new A() where x is an instance of PrintListFromTailToHead). 源代码为:

Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing[转]

最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g.  x.new A() where x is an instance of E). E指代我写的那个内部类. 根据提示,没有可访问的内部类E的实例,必须分配一

java出现No enclosing instance of type main is accessible. Must qualify the allocation with an enclosing instance of type main (e.g. x.new A() where x is an instance of main).

根据提示,没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是E的实例.)看着这句提示,我就纳闷了,我已经用new实例化了这个类,为什么还不行呢.原来我写的内部类是动态的,也就是开头以public class开头.而主程序是public static class main.在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将publ

Java中出现No enclosing instance of type XXX is accessible问题

Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: Multiple markers at this line - The value of the local variable test is not used - No enclosing instance of type StaticCallDynamic is accessible. Must qualify the allocation with an enclosing insta

Java编译时出现No enclosing instance of type XXX is accessible.

今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main). 我原来编写的源代码是这样的: public class Main {class Dog //定义一个“狗类”{privat

No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

package com.thread; public class Thread01 { public class Thread1 extends Thread{ private String name; public Thread1(String name){ this.name = name; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(name+"运行:"+i); try { s