aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes In The Matrix with Enums and Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "In The Matrix with Enums and Constructors" Watch "In The Matrix with Enums and Constructors" New topic
Author

In The Matrix with Enums and Constructors

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1



How is the constructor being called in this program? It seems like with a static reference w the constructor would not be called since it is a class variable and not an object. I know the constructor is called since count is incremented...

With enums does a constructor run whenever one of the enums is called? In this case when RAINY and SUNNY are called? Or am I missing how constructors work with static reference variables or static enum references in this case?

Respectfully,

TN

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18908
    
    8

Could you start by explaining your terminology? For example, what do you mean when you talk about "calling" an enum?
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Paul Clapham wrote:Could you start by explaining your terminology? For example, what do you mean when you talk about "calling" an enum?


Thank you for the reply. Let me explain my terminology... When I say calling an enum I mean, for instance in the program when this S.O.P. runs: System.out.print(w.RAINY.count + " " + w.SUNNY.count + " "); the w that is a static enum reference invokes the SUNNY and RAINY enum objects.

tl:dr - calling means using the dot operator to access values of the enum object similar to invoking a method in a program or using the dot operator to access instance variables that are not encapsulated.

Regards,

TN
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18908
    
    8

Ah, okay. So you're talking about when you use the enum.

By the way you're confusing yourself by using that evil feature of Java which allows you to refer to a static member of a class via an instance variable of the class. The compiler (or perhaps the runtime) converts your "w.SUNNY" to "Weather.SUNNY" under the covers, and I would advise you to use the "Weather.SUNNY" form in your code too.

As for whether the constructor is run every time you use an enum: of course not. It's an object. And so its constructor is only run at the time it's created. So perhaps you're asking when the enums are created? They would be created when their enclosing class is loaded. Which is normally the first time you use that class, although you really don't need to know that.

Which brings up the question, why do you need to know that?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
This may be a good exam question ? Can we instanitate an instance of Weather?
No. We won't use new to instantiate an instance of an enum.

By the way, the constructor of Weather is implicitly private. No one can instanitate it outside the enum.

You can only create a Weather instance like SUNNY inside the Weather enum.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Paul Clapham wrote:

Which brings up the question, why do you need to know that?


I need to know so that I can be the best Java programmer on the Internet, sir! Also, so I can pass SCJP/OCJP...

Why would the constructor load twice though if construction of an object only depends on the Weather class and not the enum blueprint?

Helen Ma wrote:
By the way, the constructor of Weather is implicitly private. No one can instanitate it outside the enum.

You can only create a Weather instance like SUNNY inside the Weather enum.


I was reading the section in SCJP by K & B earlier and did not notice any point about how the constructors of enums are implicitly private. Do you have a reference for this I can read?

Also, what you are saying in your second comment about creating objects through the enum? This sounds like what I am after. So, each enum is an object that calls its constructor when it is used in an output statement with a static reference?

Regards and 10 Billion Bytes of Thank-You,

TN

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
According to the study guide from Mughal & Rasumenssen, enum's constructor is implicitly private as I remember. So, it won't compile if you try to use new Weather to create a Weather instance.
You can only create Weather instance inside the enum instead.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Helen Ma wrote:According to the study guide from Mughal & Rasumenssen, enum's constructor is implicitly private as I remember. So, it won't compile if you try to use new Weather to create a Weather instance.
You can only create Weather instance inside the enum instead.


This link below says that enums must be, "... package-private or private access". (From the very bottom of the webpage it is the first 'Note').
Java Trail - Enum Types - The Java Tutorials

Im going to have to write and compile some more code to understand this more. Thanks for your help Helen.

Respectfully,

TN
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
The link says:
"The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself. "

By the way, treat enum as a special type of class. This can help you understand more about enum.
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
Ted North wrote:Why would the constructor load twice though if construction of an object only depends on the Weather class and not the enum blueprint?

The constructor will be called once per enum constant. You have two constants in your enum, so your enum constructor will be called twice.
Under the hood, the compiler will build a class out of your enum type which will roughly resemble the code below (the class that the compiler builds will actually extend java.lang.Enum):

Edit: Fixed typo in the code
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Norbert Muench wrote:




The constructor will be called once per enum constant. You have two constants in your enum, so your enum constructor will be called twice.


Hi Norbert,

Thank you for the reply and code. The quote above from your post was very enlightening. I think I understand much more about how enums work now.

Regards,

TN
Swapnil Suryakant Prabhavalkar
Greenhorn

Joined: Feb 25, 2012
Posts: 23

Hi All,

I would like to ask one question here.

he output of first SOP is c c 1 1

Why this 1 is printed two times?


Oracle Certified Professional, Java SE 6 Programmer (81%), Oracle Certified Expert, Java EE 6 Web Component Developer (87%)
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
Because count is an instance variable. Therefore both enum objects (Weather.RAINY and Weather.SUNNY) carry their own copy, each with the value 1 (initialized with 0 and incremented by 1 in the constructor).
 
jQuery in Action, 2nd edition
 
subject: In The Matrix with Enums and Constructors