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

Enum illegal forward reference (compiles in Java 5 but not Java 6)

 
Mark Tyler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason the following code compiles fine in Java 5 but not Java 6.

Java 6 gives the following errors:


This code has compiled fine with Java 5 and earlier versions. Also Eclipse's built-in Java compiler produces no errors on the above code.

Any suggestions on how to maintain the functionality with a solution which works for Java 6?
Also, can anyone point to any Oracle Java 6 documentation that relates to a change which would introduce this compile error?

Thanks!
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the Java version. It is a very simple error I have encountered myself. If you have something like this :


The testFrame has not been initialized, so the JDK says "I don't know what the variable is yet, because it hasn't been created." This is the same as your problem. Personally, I have not worked with enums (made my own "enums" with Strings), but I think you need to do something like this :


John Price

EDIT : I did a google search. My code above may not be correct... I found something about enums being "type-safe" in Java 5 and above :
http://www.javapractices.com/topic/TopicAction.do?Id=1
I have heard of this before.

http://www.javaworld.com/javaworld/javatips/jw-javatip122.html
http://www.javaworld.com/javaworld/javatips/jw-javatip133.html
http://download.oracle.com/javase/1,5.0/docs/guide/language/enums.html
http://blog.deepincode.com/2006/11/type-safe-enumerations-in-java-50/
http://www.javacamp.org/designPattern/enum.html

I feel these should be enough. If not, just google "type-safe enums Java".
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is simply a case of the compiler being too stupid to understand what you want to do. The "illegal forward reference" is simply that when the compiler is looking at the code, the "forward reference" has not yet been seen, so its not defined. You can fix it by reordering your code.

Most of the time, the Java compiler is smart enough to internally handle it. But in this case, it fails. Its compiler version dependent, it may change with Java 7 or Java 8 or ....
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:You can fix it by reordering your code.

Except with enums, because the enum constants need to be the first thing in the code.

A workaround is usually to use a helper class:
The reason it didn't work before is the way enums are handled when the class is loaded: first the enum constants are created, and any static fields are only instantiated after that. The enum constants cannot access anything that has not been initialized yet.
With this workaround, the enum constants ask for the Orientation fields. This causes the Orientation class to be loaded. This class is independent of the enum class, so its static fields are initialized without a problem. These can then be used in the enum constants.
 
Mark Tyler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rob, your solution is just what I was looking for. It compiles perfectly in Java 5, 6, and 7.

I believe you are correct, Pat. The Java 6 compiler seems "stupider" with its more limited range in resolving this type of forward reference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic