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 //定义一个“狗类”
{
private String name;
private int weight;
public Dog(String name, int weight) 
{
this.setName(name);
this.weight = weight;
}
public int getWeight() 
{
return weight;
}
public void setWeight(int weight) 
{this.weight = weight;}
public void setName(String name)
{this.name = name;}
public String getName() 
{return name;}
}
public static void main(String[] args)
{
Dog d1 = new Dog("dog1",1);

}
}

出现这个错误的时候,我一直不太理解。

在借鉴别人的解释之后才恍然大悟。

在代码中,我的Dog类是定义在Main中的内部类。Dog内部类是动态的内部类,而我的main方法是static静态的。

就好比静态的方法不能调用动态的方法一样。

有两种解决办法:

第一种:

将内部类Dog定义成静态static的类。

第二种:

将内部类Dog在Main类外边定义。

修改后的代码:

第一种:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

public class Main

{

    public static class Dog

    {

        private String name;

        private int weight;

        public Dog(String name, int weight)

        {

            this.setName(name);

            this.weight = weight;

        }

        public int getWeight()

        {

            return weight;

        }

        public void setWeight(int weight)

        {this.weight = weight;}

        public void setName(String name)

        {this.name = name;}

        public String getName()

        {return name;}

    }

    public static void main(String[] args)

    {

        Dog d1 = new Dog("dog1",1);

    }

}

第二种:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

public class Main

{

    public static void main(String[] args)

    {

        Dog d1 = new Dog("dog1",1);

    }

}

class Dog

{

        private String name;

        private int weight;

        public Dog(String name, int weight)

        {

            this.setName(name);

            this.weight = weight;

        }

        public int getWeight()

        {

            return weight;

        }

        public void setWeight(int weight)

        {this.weight = weight;}

        public void setName(String name)

        {this.name = name;}

        public String getName()

        {return name;}

}

 

时间: 2024-10-04 12:03:09

Java编译时出现No enclosing instance of type XXX is accessible.的相关文章

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的问题

有些时候我们要把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 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{ /*p

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 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 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 E 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的实例,必须分配一

android No enclosing instance of type BasicActivity is accessible. Must qualify the allocation with

No enclosing instance of type BasicActivity is accessible. Must qualify the allocation with an enclosing instance of type BasicActivity (e.g. x.new A() where x is an instance of BasicActivity). 描述: 在BasicActivity.java中写了一个public class 的内部类,在另一个java文件