I’ve noticed enums introduce many additional class files (Class$1) after compilation bloating the total size. It seems to be attached to every class that even uses an enum, and these are often duplicated.
Why does this occur and is there a way to prevent this without removing the enum.
(Reason for question is space is at a premium for me)
EDIT
On investigating the issue further, Sun’s Javac 1.6 creates an additional synthetic class each time you use a switch on an Enum. It uses some kind of SwitchMap. This site has some more information, and here tells you how to analyse what Javac is doing.
An additional physical file seems a high price to pay each time you use a switch on an enum!
Interestingly, Eclipe’s compiler does not produce these additional files. I wonder if the only solution is to switch compilers?
I was just bit by this behavior and this question showed up when Googling. I thought I’d share the little bit of extra information I found out.
javac 1.5 and 1.6 create an additional synthetic class each time you use a switch on an enum. The class contains a so-called “switch map” which maps enum indices to switch table jump numbers. Importantly, the synthetic class is created for the class in which the switch occurs, not the enum class.
Here’s an example of what gets generated:
EnumClass.java
EnumUser.java
Synthetic EnumUser$1.class
This switch map is then used to generate an index for a
lookupswitchortableswitchJVM instruction. It converts each enum value into a corresponding index from 1 to [number of switch cases].EnumUser.class
tableswitchis used if there are three or more switch cases as it performs a more efficient constant-time lookup vs.lookupswitch‘s linear search. Technically speaking javac could omit this whole business with the synthetic switch map when it useslookupswitch.Speculation: I don’t have Eclipse’s compiler on hand to test with but I imagine that it doesn’t bother with a synthetic class and simply uses
lookupswitch. Or perhaps it requires more switch cases than the original asker tested with before it “ugprades” totableswitch.