简体   繁体   中英

How to write an enum as a class?

In my project, I manipulate instances of the following class :

public class MyClass implements Serializable{

    private MyEnum model;
    
    private Object something; //NOT TRANSIENT
    
    public MyClass(MyEnum model){
        
        this.model = model;
        //something = ...
        
    }
    
}

Every instance of MyClass is created by passing it a model

public enum MyEnum{
    
    A,
    B,
    C,
    //A lot more of these...
    ;
    
}

I often have to serialize/deserialize instances of MyClass , this is why I marked its field something as NOT transient.

The behavior is such that :

  1. During serialization, something and the "identifier" of model will be serialized.

  2. During deserialization, something will be deserialized and model will be equal to what it was before, without having to deserialize it since it's an enum.

But I have so many of these models that I'm getting for my enum :

"The code for the static initializer is exceeding the 65535 bytes limit"

What is the proper way to fix this problem? How to write this enum as a class to work around this limitation?

Having such large enum s does not make sense. Don't you think that you have too much data in there?

You are using enums as a way to map between unique object instances and string names. So replace your enums with another type, for example, String .

TL;DR

You cannot use enum for what you require. It does not work. You could use an ArrayList or array with Strings which is filled through a file or db .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM