Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum Flow Problem

 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am not getting the flow of the program.. please help me out with the flow.
i expected c 1 c 1.. but the output is output is "c c 1 1".. how is it possible?
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As soon as the line containing the system.out.print command is reached, the enum constants are initialized. All the constants are initialized at once, whether or not they are ever referenced anywhere else.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All enum constants are created immediately when the class is loaded. So when you call w.RAINY (which should be Weather.RAINY), the Weather class is loaded and both RAINY and Sunny are created. Only then will w.RAINY.count and w.Sunny.count be evaluated.
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:All enum constants are created immediately when the class is loaded. So when you call w.RAINY (which should be Weather.RAINY), the Weather class is loaded and both RAINY and Sunny are created. Only then will w.RAINY.count and w.Sunny.count be evaluated.

I can see how the count var gets set to 1 on each pass of the enum constructor, Rob, but how does it persist after control leaves the enum and passes to main()? I can't even see the enum "instance" (if that is the right concept) in the debugger, let alone the count value.

Also, I see no way to set a useful counter in an enum as you can't reference a static field from the constructor.

p
 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It persists exactly as any class-level variable in a non-enum class would persist. If I wrote



then you can see how the value of the "value" variable persists after the constructor has been run, no? It's exactly the same for an enum class; each of the enum values is (like) an instance of the class, and the constructor is allowed to set the value of instance variables just like in any other class.

 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Witten wrote:I can't even see the enum "instance" (if that is the right concept) in the debugger, let alone the count value.

What debugger are you using? Try writing some code that stores a reference to an enum value in some other field, e.g.

Put a break point after that line, or set a watch on the variable w, and look at the value stored there.

Paul Witten wrote:Also, I see no way to set a useful counter in an enum as you can't reference a static field from the constructor.

Well, you can modify a static field if you create a static helper method to do it:

This is probably unnecessary though. Why do you feel you need a count? Are you aware of the ordinal() instance method and values() class method that any enum class will have? You should be able to get what you want from either ordinal() or values().length.
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:This is probably unnecessary though. Why do you feel you need a count? Are you aware of the ordinal() instance method and values() class method that any enum class will have? You should be able to get what you want from either ordinal() or values().length.

Mike, my interest in the "count" thing was purely pedantic (I am not the OP), and as I have had little exposure to enum I wasn't even aware of the methods you mentioned.

But I do see that a field can persist exactly the same way as a class, so that "duh" moment has passed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic