This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing enums" Watch "Accessing enums" New topic

Accessing enums

midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Hi guys! I was trying out enums. The following code works fine.

Output is

Now next i was trying to use enum CoffeeSize directly. Here is the code

It shows me the following error.

What does it mean? I din't understand why CoffeeSize is not initialized here and what is happening in previous code regarding this.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4101

This has nothing to do with enums.

You may not access a variable before it's initialized. Member variables get initialized to a default value automatically, if you don't assign a value to them. Local variables do not, you always have to assign a value to them explicitly.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Accessing enums
It's not a secret anymore!