Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange but legal enum declaration

 
Alexander Shrago
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic