user4903
user4903

Reputation:

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 (when accessed), and are there performance concerns?

Upvotes: 0

Views: 76

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500235

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.

Upvotes: 2

Related Questions