Java编译时出现No enclosing instance of type XXX is accessible.
2015-04-11 18:05
651 查看
今天在编译Java程序的时候出现以下错误:
NoenclosinginstanceoftypeMainisaccessible.MustqualifytheallocationwithanenclosinginstanceoftypeMain(e.g.x.newA()wherexisaninstanceofMain).
我原来编写的源代码是这样的:
publicclassMain
{
classDog//定义一个“狗类”
{
privateStringname;
privateintweight;
publicDog(Stringname,intweight)
{
this.setName(name);
this.weight=weight;
}
publicintgetWeight()
{
returnweight;
}
publicvoidsetWeight(intweight)
{this.weight=weight;}
publicvoidsetName(Stringname)
{this.name=name;}
publicStringgetName()
{returnname;}
}
publicstaticvoidmain(String[]args)
{
Dogd1=newDog("dog1",1);
}
}
出现这个错误的时候,我一直不太理解。
在借鉴别人的解释之后才恍然大悟。
在代码中,我的Dog类是定义在Main中的内部类。Dog内部类是动态的内部类,而我的main方法是static静态的。
就好比静态的方法不能调用动态的方法一样。
有两种解决办法:
第一种:
将内部类Dog定义成静态static的类。
第二种:
将内部类Dog在Main类外边定义。
修改后的代码:
第一种:
?
第二种:
?
NoenclosinginstanceoftypeMainisaccessible.MustqualifytheallocationwithanenclosinginstanceoftypeMain(e.g.x.newA()wherexisaninstanceofMain).
我原来编写的源代码是这样的:
publicclassMain
{
classDog//定义一个“狗类”
{
privateStringname;
privateintweight;
publicDog(Stringname,intweight)
{
this.setName(name);
this.weight=weight;
}
publicintgetWeight()
{
returnweight;
}
publicvoidsetWeight(intweight)
{this.weight=weight;}
publicvoidsetName(Stringname)
{this.name=name;}
publicStringgetName()
{returnname;}
}
publicstaticvoidmain(String[]args)
{
Dogd1=newDog("dog1",1);
}
}
出现这个错误的时候,我一直不太理解。
在借鉴别人的解释之后才恍然大悟。
在代码中,我的Dog类是定义在Main中的内部类。Dog内部类是动态的内部类,而我的main方法是static静态的。
就好比静态的方法不能调用动态的方法一样。
有两种解决办法:
第一种:
将内部类Dog定义成静态static的类。
第二种:
将内部类Dog在Main类外边定义。
修改后的代码:
第一种:
相关文章推荐
- Java在编译运行时出现No enclosing instance of type XXX is accessible问题
- Java中出现No enclosing instance of type XXX is accessible问题
- Java中出现No enclosing instance of type XXX is accessible问题
- Java中出现No enclosing instance of type XXX is accessible问题
- Java编译时出现 No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- Java中出现No enclosing instance of type XXX is accessible问题
- JAVA编译错误 “No enclosing instance of type XXX is accessible”的解决方法
- Java编译时出现 No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- Java中出现No enclosing instance of type XXX is accessible问题
- 验证参数传递引发的Java编译错误“No enclosing instance of type XXX is accessible" 及匿名内部类
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleT
- 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
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- java编译错误No enclosing instance of type TestFrame is accessible. Must qualify the allocation with an enclosing instance of type TestFrame (e.g. x.new A(
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleT
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh