• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works, too:

You can see for yourself here: https://repl.it/Mb9D/1
 
Daniele Barell
Ranch Hand
Posts: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10953
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 95
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
You can see for yourself here: https://repl.it/Mb9D/1



cute!
 
Paul Clapham
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic