我有以下代码:
class Hello {
class Thing {
public int size;
Thing() {
size = 0;
}
}
public static void main(String[] args) {
Thing thing1 = new Thing();
System.out.println("Hello, World!");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Thing什么都不做,但我的Hello,World程序在没有它的情况下编译得很好.这只是我定义的类失败了.
它拒绝编译.我开始No enclosing instance of type Hello is accessible."创造一个新的东西.我猜是:
有任何想法吗?
在Java中,您可以在单个文件中定义多个顶级类,只要其中一个是公共的(参见JLS§7.6).见下面的例子.
是否有此技术整洁名(类似于inner,nested,anonymous)?
JLS表示系统可能会强制执行这些辅助类不能的限制referred to by code in other compilation units of the package,例如,它们不能被视为包私有.这真的在Java实现之间发生了变化吗?
例如,PublicClass.java:
package com.example.multiple;
public class PublicClass {
PrivateImpl impl = new PrivateImpl();
}
class PrivateImpl {
int implementationData;
}
Run Code Online (Sandbox Code Playgroud)