如何在java中声明静态类?eclipse希望我从声明中删除"静态".
static public class Constants {
Run Code Online (Sandbox Code Playgroud)
首先回答你的问题:
只能将Nested类声明为static.顶级类不能声明为静态.
其次,Inner类是一个未显式声明为static的嵌套类.请参阅java语言规范.因此,与此处的一些答案相反,内部类不能是静态的
引用规范中的一个例子:
class HasStatic{
static int j = 100;
}
class Outer{
class Inner extends HasStatic{
static final int x = 3; // ok - compile-time constant
static int y = 4; // compile-time error, an inner class
}
static class NestedButNotInner{
static int z = 5; // ok, not an inner class
}
interface NeverInner{} // interfaces are never inner
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6231 次 |
| 最近记录: |