In Java are the object instances for field constants that represent an enum type instantiated only when first accessed?
How are enums handled when it comes to the heap, memory, and when an enum type instance is created? If I've got an enum with fifty field constants, then do I have fifty objects on the heap representing that enum type (wh开发者_如何学编程en accessed), and are there performance concerns?
The first time the type is accessed and it gets initialized, a new object is created for each value. However, unless you have a huge number of instance fields in the enum, each object will be very small. I'd be very surprised to see a situation in which this was a real performance concern unless you were on a massively constrained device.
精彩评论