今天在编译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. Extjs视频

    Extjs视频http://www.youku.com/playlist_show/id_19343298.html ExtJs视频教程(关东升) 智捷关东升老师ExtJs视频教程AJAX框架-Ext ...

  2. bluehost 邮箱设置问题

    问题描述: e-elitech.com域名,elitechus.com域名均在阿里云注册,en.e-elitech.com解析到bluehost虚拟主机,www.elitechus.com也解析到bl ...

  3. Struts2语法--Ognl

    OGNL: Object Graph Navigation Language index.jsp: <body> 访问属性 <a href="<%=contextPa ...

  4. linux仅修改文件夹权限;linux 分别批量修改文件和文件夹权限

    比如我想把/var/www/html下的文件全部改成664,文件夹改成775,怎么做呢 方法一: 先把所有文件及文件夹改成664,然后把所有文件夹改成775 root@iZ25bq9kj7yZ:/ c ...

  5. 利用transform制作幻灯片

    html代码 <html><head></head><body><div class='hpic'> <ul style=" ...

  6. ACL in 和 out 区别 (重要)

    acl中in和out的区别   in和out是相对的,比如: A(s0)-----(s0)B(s1)--------(s1)C   www.2cto.com   假设你现在想拒绝A访问C,并且假设要求 ...

  7. CF 389 E 贪心(第一次遇到这么水的E)

    http://codeforces.com/contest/389/problem/E 这道题目刚开始想的特别麻烦...但是没想到竟然是贪心 我们只需要知道偶数的时候可以对称取的,然后奇数的时候没次取 ...

  8. Spring Quartz定时器 配置文件详解

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  9. Android----ListView入门知识--各种Adapter配合使用

    引自:http://www.cnblogs.com/playing/archive/2011/03/21/1990555.html 在android开发中ListView是比较常用的组件,它以列表的形 ...

  10. Web安全检测工具的使用.

    Nikto2 Nikto2是一款使用perl语言写的多平台扫描软件,是一款命令行模式的工具,它可以扫描指定主机的WEB类型 主机名.特定目录.Cookie.特定CGI漏洞.XSS漏洞.sql注入漏洞. ...