nested class 有一个静态变量?
nested class has a static variable?
我知道嵌套class没有静态成员,
class OuterClass {
class InnerClass {
private static int x = 0; // this is error
}
}
但是当我们用 final 声明它时
class OuterClass {
class InnerClass {
private static final int x = 0; // ok
}
}
是不是和final关键字有关,因为变量不能再改变了?
或者还有其他原因吗?
因为private static final int x = 0;
是编译时常量。
根据 java 文档,
Inner classes may not declare static members, unless they are
compile-time constant fields
此行为由 Java Language Specification:
8.1.3. Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static.
An inner class may be a non-static member class (§8.5), a local class (§14.3), or an anonymous class (§15.9.5). A member class of an interface is implicitly static (§9.5) so is never considered to be an inner class.
It is a compile-time error if an inner class declares a static initializer (§8.7).
It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).
An inner class may inherit static members that are not constant variables even though it cannot declare them.
A nested class that is not an inner class may declare static members freely, in accordance with the usual rules of the Java programming language.
Example 8.1.3-1. Inner Class Declarations and Static Members
class HasStatic {
static int j = 100;
}
class Outer {
class Inner extends HasStatic {
static final int x = 3; // OK: constant variable
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
}
请注意,这随着 Java 16 发生了变化:
All of the rules that apply to nested classes apply to inner classes. In particular, an inner class may declare and inherit static
members (§8.2), and declare static initializers (§8.7), even though the inner class itself is not static
.
我知道嵌套class没有静态成员,
class OuterClass {
class InnerClass {
private static int x = 0; // this is error
}
}
但是当我们用 final 声明它时
class OuterClass {
class InnerClass {
private static final int x = 0; // ok
}
}
是不是和final关键字有关,因为变量不能再改变了? 或者还有其他原因吗?
因为private static final int x = 0;
是编译时常量。
根据 java 文档,
Inner classes may not declare static members, unless they are compile-time constant fields
此行为由 Java Language Specification:
8.1.3. Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static.
An inner class may be a non-static member class (§8.5), a local class (§14.3), or an anonymous class (§15.9.5). A member class of an interface is implicitly static (§9.5) so is never considered to be an inner class.
It is a compile-time error if an inner class declares a static initializer (§8.7).
It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).
An inner class may inherit static members that are not constant variables even though it cannot declare them.
A nested class that is not an inner class may declare static members freely, in accordance with the usual rules of the Java programming language.
Example 8.1.3-1. Inner Class Declarations and Static Members
class HasStatic { static int j = 100; } class Outer { class Inner extends HasStatic { static final int x = 3; // OK: constant variable 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 }
请注意,这随着 Java 16 发生了变化:
All of the rules that apply to nested classes apply to inner classes. In particular, an inner class may declare and inherit
static
members (§8.2), and declare static initializers (§8.7), even though the inner class itself is notstatic
.