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 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

Enum member variable access

Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109

Output of above code gives

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: 15093

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 8 API documentation
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:
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:
subject: Enum member variable access
It's not a secret anymore!