aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the default value of an enum? 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 "What is the default value of an enum?" Watch "What is the default value of an enum?" New topic
Author

What is the default value of an enum?

Pujan Kafle
Greenhorn

Joined: Sep 29, 2010
Posts: 3
I have been baffled by K&B Chapter I question 4. about enums.



For some reason, when I first answered this question, I selected the answer A (i.e. woof burble)
But after studying in preparation of OCPJP, when I revisited this question, I thought what would be the default value of an enum?
In TestEnum, Animals has only been declared but not assigned to any value. So, I thought a would be null.
Can anyone please give a nice clarification regarding the default value of an enum?

I would really appreciate it.

Thanks.
Pujan Kafle
Greenhorn

Joined: Sep 29, 2010
Posts: 3
After thinking for a while, I'll try to give it a shot.

Is it because, Animals has been declared as static and behind the scenes calling a.DOG.sound translates to Animals.DOG.sound?

Maybe my mind has become saturated and I have been looking everything with a captious mind and not been able to figure out subtle details.

I would appreciate your suggestions.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Pujan Kafle wrote:Is it because, Animals has been declared as static and behind the scenes calling a.DOG.sound translates to Animals.DOG.sound?

Yes you are right that a.DOG.sound would be converted to Animals.DOG.sound but not because a is declared static. Its because enum variables are all static. Basically an enum is translated into a normal Java class which extends java.lang.Enum. So your enum after compilation would become something like this
And as you might know, if we access any static members of a class using a reference variable of the class (in your case its a), then the compiler replaces the name of the reference variable with the class name (in your case a would be replaced by Animala)...
Pujan Kafle
Greenhorn

Joined: Sep 29, 2010
Posts: 3
Thank you very much Ankit for your clarification. This clears all the confusion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the default value of an enum?