• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble assigning enum keys to EnumMap in Java 5.0

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic