views:

171

answers:

4

Hi all,
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

+8  A: 

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.

Nikita Rybak
Which means... They're on the heap.
romacafe
+5  A: 

Since Java enums extend java.lang.Enum, they are created on the heap like all other Java objects.

duffymo
Technically, they do extend `Enum`. But it's their status as objects (ie: their indirectly extending `Object` as well) that'd make them reference types.
cHao
@0xA3: They extend `java.lang.Enum` which itself extends `java.lang.Object`.
LukeH
No, 0xA3, I mean what I said. Look at the javadocs link I posted. They extend java.lang.Enum, which in turn extends java.lang.Object.
duffymo
+2  A: 

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.

Tom Hawtin - tackline
first sentence - "so they are on the stack" - I think you meant to type "so they are on the heap" ;)
serg10
@serg10 Yes. Didn't want that # either.
Tom Hawtin - tackline
+2  A: 

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.

Jay