今天在编译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;}
}

 

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

  1. 【转】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 ...

  2. 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   ...

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

    原文:http://blog.csdn.net/sunny2038/article/details/6926079 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...

  4. No enclosing instance of type test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an

    在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...

  5. No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

    Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: No enclosing instance of type Test is accessibl ...

  6. 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A

    用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...

  7. No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)

    之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...

  8. No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).

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

  9. No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

    今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...

随机推荐

  1. CSS3边框

    border-radius box-shadow border-image border-radius可以创建圆形,如果一个元素是正方形,则设置border-radius为边长,则为圆形 box-sh ...

  2. L10,not for jazz

    expressions: It is called a clavichord这被称为古钢琴 a friend of my father's我父亲的朋友   words: musical,adj,音乐的 ...

  3. 使用Retrofit和RxJava

    使用Retrofit和RxJava整合访问网络,然后将数据显示到界面上 def retrofitVersion = '2.0.0-beta1' dependencies { compile fileT ...

  4. PHP学习笔记之数组篇

    摘要:其实PHP中的数组和JavaScript中的数组很相似,就是一系列键值对的集合.... 转载请注明来源:PHP学习笔记之数组篇   一.如何定义数组:在PHP中创建数组主要有两种方式,下面就让我 ...

  5. 手机号码抽奖系统(JS)

    <html><head><title>手机号码抽奖</title><meta http-equiv="Content-Type" ...

  6. 选择LDO的方法(转)

    http://www.micro-bridge.com/news/sort.asp?dy1=技术资料&dy2=产品相关资料&page=2 作者:LANDA PHAM   来源:德州仪器 ...

  7. Request 地址栏传值

    request页面 protected void btnSearch_Click(object sender, EventArgs e) { Response.Redirect("Reque ...

  8. 关于自定义jar包(tomcat)的添加

    1 鼠标右击工程 选择 properties 或者 Ait + Enter 2 选择Libraries 3 点击Add Library... 4 选择User Library  点击 Next 5 如 ...

  9. Recover Polygon (easy)

    Recover Polygon (easy) The zombies are gathering in their secret lair! Heidi will strike hard to des ...

  10. MJExtention

    + (NSDictionary *)mj_objectClassInArray { // key : 属性名 // value : 类名 return @{ @"dogs" : @ ...