Cases and possibilities of using Java enums mutes?

综合编程 2018-01-01

I don't know if I'm the only one to know that, but the values of an enum are not implicitly final and can be modified.

enum EnumTest {
    TOTO("TOTO 1"),
    TATA("TATA 2"),

    private String str;

    private EnumTest(String str) {
      this.str = str;

    public String toString() {
      return str;

  public static void main(String[] args) {
    EnumTest.TATA.str = "newVal";


These values are oftenly initialized at instance creation ( TOTO("TOTO 1")
) but, except myself, I've never seen anyone using the final keyword for enum variables that should be immutable. That's not the point of the question, just wondering if I'm the only one aware of this.

What I'd like to know is if there was any usecase to create mutable enums?

And I'd also like to know the limits of what we can do with enums (good practice or not). I've not tested it, but maybe an enum can be injected with Spring beans? At least it seems we can annotate each instance (@Deprecated works fine for exemple), and also the methods.

One possible usecase would be lazy initialization (calculate some field values when they are first used, if often they are not used at all), or a "normal" mutable singleton object (like a registry or such).

In most cases, though, enum objects should be immutable, and their fields be final.

Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!


踩踩鸿洋大神的坑,优化修复他Cookie支持带来的BUG... 前言:现在APP内网络框架使用Retrofit应该是很普遍的现象了,但是有时候由于业务的需求,需要维持登录状态,或者服务端需要往客户端写一些数据,然后下一次请求...
looking for Java stacktrace samples I am currently working on log normalization as well as improvements for rsyslog'...
Macaca自动化之Android测试 Macaca PC端 Web自动化测试非常类似于Selenium,而移动端自动化测试非常类似于Appium,如果你搭建过Appium环境,Macaca移动端环境...
微服务应用日志处理与组件封装 微服务应用日志组件定制 随着微服务等设计理念在系统中的应用,业务的调用链越来越复杂,日志信息越来越大,从快速增长的日志数据流中提取出所需的信息...
Download a file via HTTP Get in java I've written a download Servlet to return a file based on a messageID paramet...