aspose file tools*
The moose likes Java in General and the fly likes Enum Flow Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enum Flow Problem" Watch "Enum Flow Problem" New topic
Author

Enum Flow Problem

Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223



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?


OCPJP
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Oct 10, 2012
Posts: 86
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum Flow Problem