1.static 关键字

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

Q:

I have the following code:

   1: class Hello {

   2:     class Thing {

   3:         public int size;

   4:  

   5:         Thing() {

   6:             size = 0;

   7:         }

   8:     }

   9:  

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

  11:         Thing thing1 = new Thing();

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

  13:     }

  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: static class Thing

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

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

       2: {

       3:     Hello h = new Hello();

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

       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. 解读Google分布式锁服务

    解读Google分布式锁服务  背景介绍 在2010年4月,Google的网页索引更新实现了实时更新,在今年的OSDI大会上,Google首次公布了有关这一技术的论文. 在此之前,Google的索引更 ...

  2. Treasure Exploration(二分最大匹配+floyd)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7455   Accepted: 3 ...

  3. IIS7.0+SqlServer2012,进行.net网站发布的安装全过程

    1..net3.5安装(sqlserver2012需要) 控制面板-->管理工具-->服务器管理器-->功能-->添加功能-->选择".NET Framewor ...

  4. Oracle AWR 报告详解

    转自:http://blog.csdn.net/laoshangxyc/article/details/8615187 持续更新中... Oracle awr报告详解 DB Name DB Id In ...

  5. [转]TOMCAT原理以及处理HTTP请求的过程、ContextPath ServletPath

    一.TOMCAT 1 - Tomcat Server的组成部分 <Server>     <Service>         <Connector/>        ...

  6. django中上传图片的写法

    view参数 @csrf_exemptdef before_upload_avatar(request):    before = True    return render_to_response( ...

  7. Week5(10月11日):国庆后补课的复杂心情

    Part I:提问  =========================== 1.说说你所知道的强类型视图HTML扩展方法. 2.请解释代码. @Html.ActionLink("链接文字& ...

  8. java的new BufferedReader(new InputStreamReader(System.in))

    流 JAVA /IO 基本小结 通过一行常见的代码讨论:new BufferedReader(new InputStreamReader(System.in)) /*** *** 看到这篇文章挺好的, ...

  9. Java CopyOnWriteArrayList分析

    CopyOnWriteArrayList是一种线程安全的ArrayList,顾名思义,它会利用写时拷贝技术,它对共享对象做仅仅读操作的时候,大家都用一个共享对象,假设有可变的操作时,就会复制一份出来, ...

  10. Lua学习笔记6:C++和Lua的相互调用

        曾经一直用C++写代码.话说近期刚换工作.项目组中的是cocos2dx-lua,各种被虐的非常慘啊有木有.     新建cocos2dx-lua项目.打开class能够发现,事实上就是C++项 ...