为什么Java不强制要求在枚举属性中使用final关键字。

huangapple go评论69阅读模式
英文:

Why Java don't force to use final with enum properties

问题

让我们来看一下这里的枚举定义:

public enum Day {
    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3);

    int index;

    Day(int i) {
        index = i;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public static void main(String[] args) {
        Day x = MONDAY;
        Day y = MONDAY;
        x.setIndex(2);
        System.out.println(y.index);  // 输出:2
    }
}

一般来说,我知道我们不应该像这样实现代码。
为了防止这种情况,为什么 Java 不像处理接口属性那样,在 final int index 上使用 final 关键字。有人可以解释一下吗?

英文:

Let's take a look with my enum definition here:

public enum Day {
    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3);

    int index;

    Day(int i) {
        index = i;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public static void main(String[] args) {
        Day x = MONDAY;
        Day y = MONDAY;
        x.setIndex(2);
        System.out.println(y.index);  // Ouput: 2
    }

In general, I know we should not implement code like that.
To prevent this, why java don't use final for final int index like Java treat with interface's properties. Does anyone can explain that?

答案1

得分: 4

Questions like "Why Java doesn't force to use of final for enum properties" have only one correct answer:

Because the Java designers did not design it that way.

We can only speculate as to why they didn't design it that way. The only people who really know the actual reasons are the designers themselves. And the chances are that even they wouldn't be able to remember all of the details of their decision making. (I wouldn't be able to.) There could, in theory, still be minutes of design meetings sitting in the bottom of someone's filing cabinet, but they are not accessible to us... now.

My speculation is that the designers would have thought of plausible use-cases where enum values with mutable properties would be useful. And as @Sweeper points out, an enum-based implementation of the singleton design pattern is one such use-case. Even the example that @aeberhart quotes in his answer can be read another way:

While fields of an enum do not have to be final, in the majority of cases, we don't want our labels to change.

implies that in a minority of cases, they could want the labels to change. That's an argument for NOT requiring the label field in the example (and fields in general) to always be final... at the language level.

To generalize, it is not a good thing for a programming language design to not support (or forbid) constructs and usage patterns just because the designers don't like it. Or just because certain (so-called) experts endorse them as "best practice".

英文:

Questions like "Why Java doesn't force to use of final for enum properties" have only one correct answer:

> Because the Java designers did not design it that way.

We can only speculate as to why they didn't design it that way. The only people who really know the actual reasons are the designers themselves. And the chances are that even they wouldn't be able to remember all of the details of their decision making. (I wouldn't be able to.) There could in theory still be minutes of design meetings sitting in the bottom of someone's filing cabinet, but they are not accessible to us ... now.

My speculation is that the designers would have thought of plausible use-cases where enum values with mutable properties would be useful. And as @Sweeper points out, enum-based implementation of the singleton design pattern is one such use-case. Even the example that @aeberhart quotes in his answer can be read another way:

> While fields of an enum do not have to be final, in the majority of cases we don't want our labels to change.

implies that in a minority of cases, they could want the labels to change. That's an argument for NOT requiring the label field in the example (and fields in general) to always be final ... at the language level.

To generalize, it is not a good thing for a program language design to not support (or forbid) constructs and usage patterns just because the designers don't like it. Or just because certain (so-called) experts endorse them as "best practice".

答案2

得分: 0

这是最佳实践,使用 final,但(不幸的是)编译器不强制执行。

这个教程 (https://www.baeldung.com/java-enum-values) 这样陈述:

“我们的标签字段是 final 的。虽然枚举的字段不一定要是 final 的,在大多数情况下,我们不希望我们的标签发生变化。符合枚举值是常量的精神,这是有道理的。”

英文:

It is best practice to use final, but (unfortunately) not enforced by the compiler.

This tutorial (https://www.baeldung.com/java-enum-values) states this as follows:

"Our label field is final. While fields of an enum do not have to be final, in the majority of cases we don't want our labels to change. In the spirit of enum values being constant, this makes sense."

huangapple
  • 本文由 发表于 2020年8月22日 12:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63532678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定