File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding 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 "Doubt regarding enum" Watch "Doubt regarding enum" New topic

Doubt regarding enum

mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
Can anyone please explain in more details that how to acces enum depending upon the where it is declared for e.g

class Coffee2 {
enum CoffeeSize {BIG, HUGE, OVERWHELMING }
CoffeeSize size;
public class CoffeeTest2 {
public static void main(String[] args) {
Coffee2 drink = new Coffee2();
drink.size = Coffee2.CoffeeSize.BIG; // enclosing class
// name required
The key points to take away from these examples are that enums can be declared
as their own class, or enclosed in another class, and that the syntax for accessing
an enum's members depends on where the enum was declared.
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Enums can be declared as their own separate class, or as a class member,
however they must not be declared within a method!

if an enum is declared as its separate class(outside a class) as in the following code:

In the above code, CoffeeSize is declared outside the class. In the line marked as 1, enum CoffeeSize's constant BIG can be directly accessed with CoffeeSize.BIG

If an enum is declared as a class member as in the following code:

Here for accessing enum CoffeeSize's constant BIG, you have to use the class name in which it is declared. In the above code CoffeeSize is declared in class Coffee2. So to access it, in the line marked as 2 the class name Coffee2 is used.
You can think it of the same way you access any other class member.

Hope this is clear!

Thanks,<br />Srilatha M
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
thanks Srilatha :-)
I agree. Here's the link:
subject: Doubt regarding enum
It's not a secret anymore!