前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >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 enclosing inst

作者头像
Frank909
发布2019-01-14 18:11:22
2.3K0
发布2019-01-14 18:11:22
举报
文章被收录于专栏:Frank909Frank909

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

最近在尝试写一Demo的时候,碰到这个异常,导致Eclipse报错。

代码语言:javascript
复制
package com.frank;


public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        A a = new A();
    }

    class A{
    }

}

当时没有注意,仔细想了想,也是合理的。main是Test类的static方法,按照常理它只能访问Test类中static资源,而class A是非static所以报错了。解决方法很简单,给class A添加static属性就可以了。

代码语言:javascript
复制
package com.frank;


public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        A a = new A();
    }

    static class A{
    }

}

当然还有另外一种方式,没有必要把A放在Test中做内部类,只要把它移到Test外部地方定义也可以解决这个问题。

代码语言:javascript
复制
package com.frank;


public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        A a = new A();
    }


}

class A{
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年03月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档