No enclosing instance of type Hello is accessible
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的更多相关文章
- 【转】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 ...
- 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 ...
- 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,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...
- 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 ...
- 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 ...
- Java编译时出现No enclosing instance of type XXX is accessible.
今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...
- 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 ...
- 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 ...
- No enclosing instance of type E is accessible.
No enclosing instance of type E is accessible. 静态方法(main)中调用内部类,会出现这样的问题: 学习了:https://www.cnblogs.c ...
随机推荐
- perl post 带上请求头
my $url='https://www.zjcap.cn/business/dispatch_post.do?action=submitAdminLogin'; my $res = $ua-> ...
- Poj 1002 487-3279(二叉搜索树)
题目链接:http://poj.org/problem?id=1002 思路分析:先对输入字符进行处理,转换为标准形式:插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树. ...
- iOS之UITableViewCell左右滑动效果
首先在 UITableViewCell.h 中声明一个代理 @protocol UITableViewCellSlideDelegate <UITableViewDelegate> @op ...
- Centos 5.5 安装Mysql5.5过程
这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅 Mysql5.5.37安装须要cmake,5.6版本号開始都须要cmake来编译,5.5以后的版本号应该也要装这个. 安装cm ...
- if语句之有房么?有钱么?有能力么?
思路:1.如果有房,可以谈谈 2.如果没有房,问第二个条件有钱么,如果有,可以谈谈 3.如果没有房没有钱,则问第三个条件有能力么,如果有,可以谈谈 4.如果以上三个条件都没有,则拜拜 Console. ...
- Jquery - checked 全选与取消全选
html: <!DOCTYPE html> <html> <head> <title>节点列表</title> <link rel=& ...
- CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Week13(12月2日):又到了那个点,期末了~~~~
Part I:提问 =========================== 1.ASP.NET MVC是微软.NET平台上的一个( ). A.语言 B.集成开发环境 C.Web开 ...
- Spring+EhCache缓存实例(详细讲解+源码下载)(转)
一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...