wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum member variable access 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 "Enum member variable access" Watch "Enum member variable access" New topic
Author

Enum member variable access

Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109


Output of above code gives
red
false


why is it giving red? shouldnt it be a compiler error or if not give some exception. please explain..

thanks in advance


SCJP 5.0<br />SCJD working on it
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

The member variable 'name' in the enum doesn't have an access specifier (public, protected, private), so it has default access - which means that other classes in the same package such as class Class can access it.

See The Java Tutorial - Controlling Access to Members of a Class

Note that an enum is a kind of special class, and the access rules for classes apply to enums as well.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Yes, also the statement:
Color a = Color.Green;
Here 'a' is reference variable of type 'Color' that refers to an enum type 'Green'. You can use it more less you use a reference variable. The statement:
System.out.println(a.Red.name);
Its ok to access another enum type using the same variable, even you can use it to refer to another enum type: a = Color.Blue;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum member variable access