Reputation: 2699
Since enum in C# are on the stack, I was wondering where enum, in Java, where created. On the stack? On the heap? In some mysterious other place?
Enumeration in C# are more primitive than those in Java, this might explain why they are created on the stack...
Where are they? I can't find them!
Thanks
Upvotes: 8
Views: 1602
Reputation: 147164
Enums are objects in Java, so they are on the heap. However, for each type there is only a fixed number of them. Client code is dealing with references to these enum objects, so doesn't actually create anything on the heap. As ever from a specification point of view: local variable references are on the stack; object field references are on the heap.
Upvotes: 4
Reputation: 27492
They are objects, just like any other object, so the enum itself is on the heap. A variable that holds a reference to an enum may be on the stack if it is a function variable, or it may be on the heap inside some other object if it is a member of an object.
Upvotes: 2
Reputation: 309008
Since Java enums extend java.lang.Enum
, they are created on the heap like all other Java objects.
Upvotes: 7
Reputation: 68036
Enums in Java are also objects: for example, enums can have instance variables/methods/constructors and implement interfaces. All this makes me think they're handled just like other objects by jvm.
Upvotes: 9