wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Enums and access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enums and access " Watch "Enums and access " New topic
Author

Enums and access

dave hopkins
Greenhorn

Joined: Oct 27, 2006
Posts: 25
I have read that when enum's are declared within a class, a separate class must use the surrounding class to access the enum. But the following compiles & runs fine.


What gives?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I get a compilation error when I try it. I suggest you check to see if you've got an old TimeZone.class file lying around somewhere in your class path, and delete it.


"I'm not back." - Bill Harding, Twister
Kail Limas
Greenhorn

Joined: Jul 09, 2007
Posts: 24
Actually the class is unnecesary. You can use the class. But then you need to add "Numnum." to the output.

Without the class you can just leave it be as is.

At least it works in Eclipse that way.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Your code runs and compiles fine, but the reason is that the enum class has now default access. So you can only use it within your package.

If you want to use your enum outside your package. You must mark your enum public and this will give a compile error because in Java you can have only one public class in a file. In that case you have to define your enum outside the class.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
 
 
subject: Enums and access
 
Similar Threads
enums
thread safe singleton class
Are instances of enums static by default?
Related to Enum const specific class body
sharing enum contants