• Post Reply Bookmark Topic Watch Topic
  • New Topic

Defining / Understanding anatomy of Enum  RSS feed

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just ran in to this problem, any rhyme or reason why the compiler barfs ?.



SNIPPET # 1 ( WORKS )

SNIPPET # 2 ( DOES NOT COMPILE ).


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at enumeffectivejava.Main.main(Main.java:10)

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think when we declare an enum the members should be the first thing that needs to be declared. Anything else might be causing the error.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like package must be the first non-comment line in a Java file, the enum members MUST be the first things declared in an enum.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check proper syntax of enums in the Java Specification:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9

An enum declaration has the form:

EnumDeclaration:
ClassModifiersopt enum Identifier Interfacesopt EnumBody

EnumBody:
{ EnumConstantsopt ,opt EnumBodyDeclarationsopt }

..................

EnumBodyDeclarations:
; ClassBodyDeclarationsopt

Enum constanst in EnumBody must be declared first, ClassBodyDeclarations must start with semicolon and must follow EnumConstants.
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Rodeo wrote:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at enumeffectivejava.Main.main(Main.java:10)

Don't run applications while there are still compiler errors. Always resolve those first. Compiler errors are more useful than "uncompilable source code".
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks much !. For this wonderful link. Very useful.

_SM

Ireneusz Kordal wrote:Check proper syntax of enums in the Java Specification:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9

An enum declaration has the form:

EnumDeclaration:
ClassModifiersopt enum Identifier Interfacesopt EnumBody

EnumBody:
{ EnumConstantsopt ,opt EnumBodyDeclarationsopt }

..................

EnumBodyDeclarations:
; ClassBodyDeclarationsopt

Enum constanst in EnumBody must be declared first, ClassBodyDeclarations must start with semicolon and must follow EnumConstants.
 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Apologies, but my intent was understanding why the ordering is such an important criterion with Enums. I was more interested in how the JVM looks up an Enum opposed to a class.

_SM

Rob Prime wrote:
Steven Rodeo wrote:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at enumeffectivejava.Main.main(Main.java:10)

Don't run applications while there are still compiler errors. Always resolve those first. Compiler errors are more useful than "uncompilable source code".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!