Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding enum

 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Srilatha :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic