• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing an enum to a field. How can it be?  RSS feed

 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers!
There's a code I came across and I cannot fully understand:


It compiles regularly and print two numbers.

What puzzles me is the following line:


What is c? Shouldn't be defaulted  as null? Or Should it be assigned to a Colors enum value like c = Colors.RED? How can it represet an entire enum?

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c is a reference to an enum, which is defined as any of the values listed: RED, GREEN, BLUE.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since enums are objects, yes, they will be initialized to null if they are class/instance members. Edit (clarification): A reference to an enum, like c, will be initialized to null if it is a class or instance member.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your confusion may stem from the misleading name given to the enum type. Just like classes, an enum is usually given a singular name, like Color in this case, not plural.
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the variable c is initialized to null and never changed to any other value. However the expression c.RED doesn't actually use the variable c. It's actually equivalent to Colors.RED; I'm not sure why the designers of Java made this legal but they did.
 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi (again) Junilu.
Thanks for all the answers you provide!
Junilu Lacar wrote:Since enums are objects, yes, they will be initialized to null if they are class/instance members. Edit (clarification): A reference to an enum, like c, will be initialized to null if it is a class or instance member.

So how can be possible to work the print output?


 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul
Paul Clapham wrote:Yes, the variable c is initialized to null and never changed to any other value. However the expression c.RED doesn't actually use the variable c. It's actually equivalent to Colors.RED; I'm not sure why the designers of Java made this legal but they did.


That's odd, isn't it?
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is. You didn't mention it but that makes me suspect your code came from something which is aimed at people taking a certification exam. I wouldn't expect to find that "feature" in real code anywhere.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java compiles an enum into something very similar to this regular class definition:

Since static members of a class can also be accessed via a variable reference, I guess it makes sense to allow access to an enum value via a variable reference. It's confusing though, as you have seen, so you should avoid doing that.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works, too:

You can see for yourself here: https://repl.it/Mb9D/1
 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, it is. You didn't mention it but that makes me suspect your code came from something which is aimed at people taking a certification exam. I wouldn't expect to find that "feature" in real code anywhere.

What an eye! :)
Actually I took and "adapted" a question from a book on OCAJP I'm studied.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, the variable c is initialized to null and never changed to any other value. However the expression c.RED doesn't actually use the variable c. It's actually equivalent to Colors.RED; I'm not sure why the designers of Java made this legal but they did.

Color.RED is the preferable way to access static members. Then you can do away with 'c' entirely.
 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
You can see for yourself here: https://repl.it/Mb9D/1


cute!
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Since static members of a class can also be accessed via a variable reference, I guess it makes sense to allow access to an enum value via a variable reference.


Yes, I'd seen the former usage before (discussed here on the Ranch) but not the latter. But yeah, I guess they are the same thing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!