gpt4 book ai didi

java - 封闭类中的私有(private)枚举和静态字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:22:12 24 4
gpt4 key购买 nike

我明白为什么枚举构造函数不能访问静态字段和枚举本身中的方法,以及为什么允许使用相同的方法在类里面。以下面的代码为例,

import java.util.ArrayList;
import java.util.List;

public enum Foo {
A("Some string"),
B("Some other string"),
;

static List<String> list = new ArrayList<>();

Foo(String description) {
list.add(description);
}
}

此代码导致编译时错误,从初始化程序非法引用静态字段

相关背景

枚举构造函数在静态字段拥有所有之前被调用被初始化。在上面的示例中,这意味着 list 尚未初始化。这是因为静态字段是在文本中初始化的订单(section 12.4.2)

Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

(强调我的)

并且由于枚举值本身总是先于任何其他值字段,包括静态字段,它们对枚举构造函数,即没有静态字段可以先于枚举值 AB

问题

但是,这是我的问题,为什么“私有(private)”(包含在类中)enum可以访问其封闭类的静态字段,无论枚举是否出现在 --- 或 --- 之前在静态字段之后?特别是,在 Java 规范中的哪个位置指定了这一点?

引用下面的代码

import java.util.ArrayList;
import java.util.List;

public class Bar {
static List<String> first = new ArrayList<>();

enum Baz {
A("Some string"),
B("Some other string"),
;


Baz(String description) {
// Can access static fields from before the enum
first.add(description);

// Can access static fields from _after_ the enum
second.add(description);
}
}

static List<String> second = new ArrayList<>();
}

最佳答案

这在 JLS 中有点到处都是。 When Initialization Occurs chapter

The intent is that a class or interface type has a set of initializers that put it in a consistent state, and that this state is the first state that is observed by other classes. The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (§8.3.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

该粗体片段指的是直接包含访问权限的类。

enum 类型在 Java 语言规范中定义,here

An enum declaration specifies a new enum type, a special kind of class type.

您在 Baz 构造函数中访问的字段

Baz(String description) {
// Can access static fields from before the enum
first.add(description);

// Can access static fields from _after_ the enum
second.add(description);
}

不是中声明的类变量,枚举类型Baz。因此允许访问。

我们可以更深入地研究 detailed class initialization procedure ,这说明每个类(类、接口(interface)、枚举)都是独立初始化的。但是,我们可以创建一个示例来查看尚未初始化的值

public class Example {
public static void main(String[] args) throws Exception {
new Bar();
}
}

class Bar {
static Foo foo = Foo.A;
static Integer max = 42;

enum Foo {
A;

Foo() {
System.out.println(max);
}
}
}

这将打印null。在 enum 类型的构造函数中允许访问 max,尽管我们的程序执行在 max 初始化之前就达到了访问权限。 JLS warns against this

The fact that initialization code is unrestricted allows examples to be constructed where the value of a class variable can be observed when it still has its initial default value, before its initializing expression is evaluated, but such examples are rare in practice. (Such examples can be also constructed for instance variable initialization (§12.5).) The full power of the Java programming language is available in these initializers; programmers must exercise some care.


您原来的 Foo 示例引入了一个额外的规则,定义在 chapter on Enum Body Declarations 中.

It is a compile-time error to reference a static field of an enum type from constructors, instance initializers, or instance variable initializer expressions of the enum type, unless the field is a constant variable (§4.12.4).

该规则会阻止编译您的 Foo 片段。

enum constants translate to public static final fields .它们以文本形式首先出现在 enum 类型定义中,因此首先被初始化。它们的初始化涉及构造函数。规则的存在是为了防止构造函数看到其他类变量的未初始化值,这些变量稍后必须初始化。

关于java - 封闭类中的私有(private)枚举和静态字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com