1.static 关键字

  • 修饰的成员被所有对象共享(包括成员变量和方法)。
  • 修饰的成员优先于对象存在。
  • 存储于方法区(共享数据区)的静态区中。
  • 静态方法只能访问静态成员。
  • 静态方法中不可以使用this或super关键字。
  • 主函数是static,只能调用static方法。
  • 静态代码块随着类的加载而运行(只执行一次)。用于给类进行初始化。

Q:

I have the following code:

  1. 1: class Hello {

  1. 2: class Thing {

  1. 3: public int size;

  1. 4: 

  1. 5: Thing() {

  1. 6: size = 0;

  1. 7: }

  1. 8: }

  1. 9: 

  1. 10: public static void main(String[] args) {

  1. 11: Thing thing1 = new Thing();

  1. 12: System.out.println("Hello, World!");

  1. 13: }

  1. 14: }

it refuses to compile. I get No enclosing instance of type Hello is accessible." at the line that creates a new Thing.

A

You've declared the class Thing as a non-static inner class. That means it must be associated with an instance of the Hello class.

In your code, you're trying to create an instance of Thing from a static context. That is what the compiler is complaining about.

There are a few possible solutions. Which solution to use depends on what you want to achieve.

  • Change Thing to be a static nested class.

    1. 1: static class Thing

  • Create an instance of Hello, then create an instance of Thing.

    1. 1: public static void main(String[] args)

    1. 2: {

    1. 3: Hello h = new Hello();

    1. 4: Thing thing1 = h.new Thing(); // hope this syntax is right, typing on the fly :P

    1. 5: }

  • Move Thing out of the Hello class.

No enclosing instance of type Hello 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 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 ...

  6. Java编译时出现No enclosing instance of type XXX is accessible.

    今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...

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

  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 E is accessible.

    No enclosing instance of type E  is accessible. 静态方法(main)中调用内部类,会出现这样的问题: 学习了:https://www.cnblogs.c ...

随机推荐

  1. 国际名品SYSTEM入驻北京金融街购物中心__购物败家_YOKA时尚网

    国际名品SYSTEM入驻北京金融街购物中心__购物败家_YOKA时尚网 国际名品SYSTEM入驻北京金融街购物中心

  2. Python 連接 MySQL

    Python 連接 MySQL MySQL 是十分流行的開源資料庫系統,很多網站也是使用 MySQL 作為後台資料儲存,而 Python 要連接 MySQL 可以使用 MySQL 模組.MySQLdb ...

  3. Android 进程和线程

    进程和线程 如果某个应用程序组件是第一次被启动,且这时应用程序也没有其他组件在运行,则Android系统会为应用程序创建一个包含单个线程的linux进程.默认情况下,同一个应用程序的所有组件都运行在同 ...

  4. 工作需要稍微研究了一下Hyper-V

    Hyper-V是指微软的虚拟化技术,底层用得Hypervisior好像没有特殊的名字 虚拟化市场占有率前三:VMWare,Hyper-V,Citrix Citrix的XenServer今年彻底开源,原 ...

  5. git 使用过程(三、文件的添加 修改)

    1.库中添加文件 在目录下新建一个文件 如 testfile.txt .输入命令:① git add testfile.txt  ②git commit -m "这里是你提交的说明" ...

  6. JQ 一些基本方法

    1.判断复选框是否有选中,bischecked 返回 ture 或 false var bischecked = $('[name=uid]').is(':checked'); 2.查看当前元素是父元 ...

  7. 数矩形(N - 暴力求解、打表)

    数矩形 Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格.            Input 第一行输入一个t, 表示有t组数据,然后 ...

  8. 使用Intel编译器获得一致的浮点数值计算结果

    使用Intel编译器获得一致的浮点数值计算结果大多数十进制的浮点数, 用二进制表示时不是完全一致的; 与此同时, 大多数与浮点数值相关的计算结果, 存在着固有的不确定性.通常, 编写浮点计算应用软件希 ...

  9. 缩略图类库--ThumbLib使用简介

    //加载类库文件 require_once 'path/to/ThumbLib.inc.php'; //实例化类库,传入你要处理的图片的地址可以是网络地址,也可以是本地地址 $thumb = PhpT ...

  10. pay包注释(二)

    @login_required()def to_register(request):    return render_to_response("pay/register_yeepay.ht ...