wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange but legal enum declaration 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 "strange but legal enum declaration" Watch "strange but legal enum declaration" New topic
Author

strange but legal enum declaration

Alexander Shrago
Greenhorn

Joined: Aug 18, 2008
Posts: 14
You can declare public static enum within some class:

public class MealAdministrator1 {
public static enum Meal1 {
BREAKFAST, LUNCH, DINNER
}
public static void main(String[] args) {
System.out.println(Meal1.BREAKFAST);
}
}

Why such public enum is visible only within MealAdministrator1?
What does static modifier mean here?
And..Where is no compiler error. Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18759
    
  40

Alexander Shrago wrote:You can declare public static enum within some class:

public class MealAdministrator1 {
public static enum Meal1 {
BREAKFAST, LUNCH, DINNER
}
public static void main(String[] args) {
System.out.println(Meal1.BREAKFAST);
}
}

Why such public enum is visible only within MealAdministrator1?
What does static modifier mean here?
And..Where is no compiler error. Why?



The Meal1 enum should be visible outside of the class -- after all it is declared as public.

The static modifiers does nothing. The JLS allows you to specify nested enum as static, even though it is implied as static.

And finally, what error were you expecting?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Why such public enum is visible only within MealAdministrator1?


Your enum is visible outside the class - MealAdministrator1.Meal1
If the enum was not static, you would still access it the same way.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: strange but legal enum declaration