Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enums and access

 
dave hopkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kail Limas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic