Two Laptop Bag
The moose likes Java in General and the fly likes Trouble assigning enum keys to EnumMap in Java 5.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Trouble assigning enum keys to EnumMap in Java 5.0" Watch "Trouble assigning enum keys to EnumMap in Java 5.0" New topic
Author

Trouble assigning enum keys to EnumMap in Java 5.0

Johnny Smith
Greenhorn

Joined: Apr 15, 2005
Posts: 3
Here is the code snippet:

File: DirProc.java
-----------------------
public class DirProc
{
public enum DIRECTION { X, Y};

// Other methods and variables as well
}


File: Test.java
--------------------
import static DirProc.DIRECTION;

public class Test
{
private Map< DIRECTION, String > myDirMap = new EnumMap< DIRECTION , String> ( DIRECTION.class ); // COMPILER ERROR HERE

}
-----> Compiler error: "Test.java": type parameter DirProc.DIRECTION is not within its bound at line 77, column 88


The compiler complains even though the key is of type DIRECTION which is an
enum, and is visible in Test.java (other methods make use of DIRECTION
without errors)


I'm out of ideas. Any help will be appreciated. Thanks a lot.
[ April 15, 2005: Message edited by: Johnny Smith ]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Leave the type bounds out of the constructor:
I know it doesn't look right, but it works.
Johnny Smith
Greenhorn

Joined: Apr 15, 2005
Posts: 3
Alan,

Compiling and running the program with that approach gives me varying results on JBuilder 2005 and command line (javac/java)



JBUILDER 2005 OUTPUT:
--------------------



1.5 javac/java OUTPUT gives me 2 warning but no Exceptions
------------------------------------------------------------


Any clues?
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
I just tried it again with the type parameters in the constructor, and it works fine. (I'm sure it didn't work before, but that may have been in one of the tiger betas.) Anyway, maybe the problem is with the argument to the constructor. Try DirProc.DIRECTION.class.

On a style note, since enums are types, like classes and interfaces, their names should be in CamelCase, not ALL_CAPS--that should be reserved for the constants (e.g., Direction.NORTH, TrafficLightState.GREEN).
Johnny Smith
Greenhorn

Joined: Apr 15, 2005
Posts: 3
Alan,

What compiler are you using to build the program?

Running the program with type parameters in the constructor worked fine when compiled with javac + java (1.5) on the command line.

The problem comes when I use JBuilder 2005 (Free Trial) to build (either Borland Make or javac). That is when I get the error:
-----> Compiler error: "EnumSingleTest.java": type parameter EnumSingleTest.RUN_MODE is not within its bound at line 77, column 88

Replacing RUN_MODE with EnumSingleTest.RUN_MODE did NOT solve the problem.

Maybe its a JBuilder incompatibility...

Thanks

[ April 16, 2005: Message edited by: Johnny Smith ]
[ April 16, 2005: Message edited by: Johnny Smith ]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
I use javac, and it works fine for me, too. In your original example, the enum appears to be an inner class of a class other than the one you're calling it from. I thought maybe using the fully-qualified name would make a difference in that case. You shouldn't have to, though; this is obviously a bug in JBuilder.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Trouble assigning enum keys to EnumMap in Java 5.0
 
Similar Threads
generics problem
null as key in HashMap ?
enum in Tiger
Simple J2SE 5.0 Tiger Notes
generic enum and the values() method