为什么允许在非静态内部 类 中将编译时间常量设为静态?
Why compile time constants are allowed to be made static in non static inner classes?
假设我们有如下代码。
public class Outer{
class Inner{
public static final String s = "abc";
}
static class Nested{
public static final SomeOtherClass instance = new SomeOtherClass();
}
}
我明白要实例化 非静态内部对象 classes 需要一个 外部 class 对象. static
表示 class 相关,访问它不需要实例化对象。非静态内部 class 只能在我们实例化外部 class 的对象后使用。在其中包含任何静态引用可能没有意义。
我的问题:
非静态内部 class 可以在没有外部 class 的任何显式对象的情况下加载吗?
为什么允许编译时间常量(字符串文字,因为它们在字符串池和原始类型中以特殊方式处理)static in non static inner class ?
编辑:为什么不能将非编译时常量设为静态,我知道它是根据 JLS,但只是想知道 哪里出了问题,这样做的意图是什么这条规则。
问题 1 - 非静态内部 class 可以在没有外部 class 的任何显式对象的情况下加载吗?
是的,您可以那样加载非静态内部 class,请看下面的示例:
class MyObject {
class InnerObject {
static final String prop = "SOME INNER VALUE";
}
static void doLoad() {
System.out.println(InnerObject.prop);
}
}
问题是没有太多理由这样做,因为非静态内部 class 不允许有任何静态块、方法、字段,如果它们不是最终的,如下面的问题。
问题 2 - 为什么我们可以将编译时常量(字符串文字,因为它们在字符串池和原始类型中以特殊方式处理)允许在非静态内部 class ?
Java
旨在让您可以在非静态内部 classes.
中使用 static
、final
字段
- Can non static inner class get loaded without any explicit object of Outer class ?
是的。创建内部 class 的 实例 需要外部 class 的实例。但是两个 classes 都可以在创建任何实例之前加载。
- Why can we have compile time constants (String literals, as they are handled in special way in String pool and primitive types) are allowed to be made static in non static inner class ?
语言规范允许对常量变量进行此例外处理。来自 Java Language Specification, section 8.1.3: "Inner classes and enclosing instances":
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).
并且字符串变量可以是常量,因为 4.12.4, "final
Variables":
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28).
假设我们有如下代码。
public class Outer{
class Inner{
public static final String s = "abc";
}
static class Nested{
public static final SomeOtherClass instance = new SomeOtherClass();
}
}
我明白要实例化 非静态内部对象 classes 需要一个 外部 class 对象. static
表示 class 相关,访问它不需要实例化对象。非静态内部 class 只能在我们实例化外部 class 的对象后使用。在其中包含任何静态引用可能没有意义。
我的问题:
非静态内部 class 可以在没有外部 class 的任何显式对象的情况下加载吗?
为什么允许编译时间常量(字符串文字,因为它们在字符串池和原始类型中以特殊方式处理)static in non static inner class ?
编辑:为什么不能将非编译时常量设为静态,我知道它是根据 JLS,但只是想知道 哪里出了问题,这样做的意图是什么这条规则。
问题 1 - 非静态内部 class 可以在没有外部 class 的任何显式对象的情况下加载吗?
是的,您可以那样加载非静态内部 class,请看下面的示例:
class MyObject {
class InnerObject {
static final String prop = "SOME INNER VALUE";
}
static void doLoad() {
System.out.println(InnerObject.prop);
}
}
问题是没有太多理由这样做,因为非静态内部 class 不允许有任何静态块、方法、字段,如果它们不是最终的,如下面的问题。
问题 2 - 为什么我们可以将编译时常量(字符串文字,因为它们在字符串池和原始类型中以特殊方式处理)允许在非静态内部 class ?
Java
旨在让您可以在非静态内部 classes.
static
、final
字段
- Can non static inner class get loaded without any explicit object of Outer class ?
是的。创建内部 class 的 实例 需要外部 class 的实例。但是两个 classes 都可以在创建任何实例之前加载。
- Why can we have compile time constants (String literals, as they are handled in special way in String pool and primitive types) are allowed to be made static in non static inner class ?
语言规范允许对常量变量进行此例外处理。来自 Java Language Specification, section 8.1.3: "Inner classes and enclosing instances":
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).
并且字符串变量可以是常量,因为 4.12.4, "final
Variables":
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28).