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. paip.php-gtk 桌面程序 helloworld总结

    paip.php-gtk 桌面程序 helloworld总结 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.cs ...

  2. PHP 自学之路-----XML编程(Xpath技术,simpleXml技术)基础入门

    XPAth技术 XPath的设计的核心思想,可以通过xpath迅速简介的定位到你希望查找的节点.主要目的是描述节点相对其他节点的位置,可以取得所有符合条件的节点,成为[位置路径]. Xapth主要用来 ...

  3. 3644 - X-Plosives(水题,并差集)

    3644 - X-Plosives A secret service developed a new kind of explosive that attain its volatile proper ...

  4. 使用ORACLE SQL Tuning advisor快速优化低效的SQL语句

    ORACLE10G以后版本的SQL Tuning advisor可以从以下四个方面给出优化方案 (1)为统计信息丢失或失效的对象收集统计信息   (2)考虑优化器的任何数据偏差.复杂谓词或失效的统计信 ...

  5. C# 方法的调用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Java-Math

    java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名:             import static java.lang.Mat ...

  7. Python 2.7 学习笔记 异常处理

    如同别的开发语言,python也支持异常处理机制.本文介绍下它的基本语法. 一.异常的基本处理框架如下: try: 业务代码 except 异常类1: 异常处理代码 except 异常类2: 异常处理 ...

  8. jquery删除动态增加的li

    <script type="text/jscript"> //楼主帮你修改调整了下 $(document).ready(function () { $('.zuo li ...

  9. “快的打车”创始人陈伟星的新项目招人啦,高薪急招Java服务端/Android/Ios 客户端研发工程师/ mysql DBA/ app市场推广专家,欢迎大家加入我们的团队! - V2EX

    "快的打车"创始人陈伟星的新项目招人啦,高薪急招Java服务端/Android/Ios 客户端研发工程师/ mysql DBA/ app市场推广专家,欢迎大家加入我们的团队! - ...

  10. 数据挖掘算法学习(三)NaiveBayes算法

    算法简单介绍 NBC是应用最广的分类算法之中的一个.朴素贝叶斯模型发源于古典数学理论,有着坚实的数学基础,以及稳定的分类效率.同一时候,NBC模型所需预计的參数非常少,对缺失数据不太敏感,算法也比較简 ...