2

So from my understanding of Java, when you write

return value;

It might actually return one of two things: the value of 'value', or a reference to 'value', depending on what type 'value' is. To my knowledge, if 'value' is a primitive data type, Java returns the value of 'value', and if 'value' is an abstract data type, like a class, then Java returns a reference to 'value'.

Now, what I'm curious about is the behavior of return with enumerations. If 'variable' is of type enum, does a value or a reference get returned?

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
MattS
  • 717
  • 2
  • 7
  • 22
  • 1
    Well, if you want to get technical about it, it will return the value of the reference, not the reference itself. – Jeffrey May 30 '12 at 00:10
  • let me advertise my answer a little bit to clarify some technical aspects that Jeffrey pointed out: http://stackoverflow.com/questions/40480/is-java-pass-by-reference/7034719#7034719 and let me try to confuse you a little bit with this question: what if value is an int[]? Hint: there are only 8 primitive data types in Java... – Marsellus Wallace May 30 '12 at 02:33

2 Answers2

8

Any Enum is an object that extends java.lang.Enum and as such you get back a reference.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • Great, thanks for clearing that up! Google was being surprisingly dodgy in answering that question for me. – MattS May 29 '12 at 23:59
1

An enum is not a primitive. There is a single unique object instance for each enum 'value' per class-loader. The return type is a reference to that unique object.

Affe
  • 47,174
  • 11
  • 83
  • 83