• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble assigning enum keys to EnumMap in Java 5.0  RSS feed

 
Johnny Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leave the type bounds out of the constructor:
I know it doesn't look right, but it works.
 
Johnny Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!