Initializing A HashMap In an Interface?

Hi,

Can I create an interface that contains a static final HashMap that I initialize to relate my static final Ineger "type" constants to a staitc final String "name".

It would be simple to use a static final String[ ] array ... that I can initlaize with {...} ..., provided that I began numbering my "type" constants from 0 .... (I'd just rather not)

My first try at code is below. I guess I cannot use a static block to initialize the HaspMap as I can in a class.

FYI: I am trying to create my own "MyCollector" class to wrap any of my 5 subclasses of [ JLable |JTextField | 3 different JComboBox subclasses].

My app builds a JPanel that is populated with 5 subclasses these components, but the number and position of each is only known at run time.

I figured I could create a MyCollector class to wrap any of the 5 unrelated subclasses. Then create an interface implemented by each of the 5 subclasses to let me manipulate them all ienditcially.

But I'd also like MyCollector to implement the same interface so they can all share some constants. [It also guarantees they all stay coordinated -- MyCollector implements all the comon methods by just calling the same method on the wrapped subclass.]

Anyway ... is it impossible to initialize a static HashMap in an interface or am I missing something?

publicinterface MyCollectorInterface{

publicstaticfinalint EMPTY_INT= 489752;

publicstaticfinalint ERROR_INT= EMPTY_INT + 1;

publicstaticfinalint LBL_INT= ERROR_INT + 1;

publicstaticfinalint TF_INT= LBL_INT + 1;

publicstaticfinalint CB_NA_INT= TF_INT + 1;

publicstaticfinalint CB_NONE_INT= CB_NA_INT + 1;

publicstaticfinalint CB_OTHER_INT = CB_NONE_INT + 1;

publicstaticfinal Integer ERROR=new Integer(ERROR_INT);

publicstaticfinal Integer EMPTY=new Integer(EMPTY_INT);

publicstaticfinal Integer LBL=new Integer(LBL_INT);

publicstaticfinal Integer TF=new Integer(TF_INT);

publicstaticfinal Integer CB_NA=new Integer(CB_NA_INT);

publicstaticfinal Integer CB_NONE =new Integer(CB_NONE_INT);

publicstaticfinal Integer CB_OTHER =new Integer(CB_OTHER_INT);

publicstaticfinal HashMap<Integer, String>

COMPONENT_TYPE_NAMES =new HashMap<Integer, String>();

static{

COMPONENT_TYPE_NAMES.put(ERROR,"ERROR");

COMPONENT_TYPE_NAMES.put(TF,"TF");

COMPONENT_TYPE_NAMES.put(CB_NA,"CB_NA");

COMPONENT_TYPE_NAMES.put(CB_NONE,"CB_NONE");

COMPONENT_TYPE_NAMES.put(CB_NONE,"CB_OTHER");

}

}

Thanks for any help.

[4992 byte] By [gberisha] at [2007-10-3 4:36:25]
# 1
Maybe you should try abstract class instead of interface,does it make more sense?
Cranea at 2007-7-14 22:40:06 > top of Java-index,Core,Core APIs...
# 2

Firstly, as you are using Java5, why not using an enum type? With enums, toString() will give you the name of the defined constant and valueOf(String) will give you the constant for a name. Plus, throughout the code, they are type safe.

To your code: you cannot use static initializers for interfaces (see Java Language Specs). The following comes quite close to what you intend to do, though, I am not sure if it is really what one should do. (Btw., members of interfaces implicitely are public static final, so I omitted those modifiers). I also added an example for a typesafe enum, you could use instead, which in many cases accomplishes the same purpose.

public interface MyCollectorInterface {

int EMPTY_INT= 489752;

int ERROR_INT= EMPTY_INT + 1;

int LBL_INT= ERROR_INT + 1;

int TF_INT= LBL_INT + 1;

int CB_NA_INT= TF_INT + 1;

int CB_NONE_INT= CB_NA_INT + 1;

int CB_OTHER_INT = CB_NONE_INT + 1;

Integer ERROR= new Integer(ERROR_INT);

Integer EMPTY= new Integer(EMPTY_INT);

Integer LBL= new Integer(LBL_INT);

Integer TF= new Integer(TF_INT);

Integer CB_NA= new Integer(CB_NA_INT);

Integer CB_NONE = new Integer(CB_NONE_INT);

Integer CB_OTHER = new Integer(CB_OTHER_INT);

Map<Integer, String> COMPONENT_TYPE_NAMES = Types.getMap();

final class Types {

private static Map<Integer, String> getMap() {

final HashMap<Integer, String> map = new HashMap<Integer, String>();

map.put(ERROR,"ERROR");

map.put(TF,"TF");

map.put(CB_NA,"CB_NA");

map.put(CB_NONE, "CB_NONE");

map.put(CB_OTHER, "CB_OTHER");

return map;

}

}

enum ComponentTypes {

ERROR,

TF,

CB_NA,

CB_NONE,

CB_OTHER

}

}

stefan.schulza at 2007-7-14 22:40:06 > top of Java-index,Core,Core APIs...
# 3

> int EMPTY_INT= 489752;

>int ERROR_INT= EMPTY_INT + 1;

> int LBL_INT= ERROR_INT + 1;

>int TF_INT= LBL_INT + 1;

> int CB_NA_INT= TF_INT + 1;

>int CB_NONE_INT= CB_NA_INT + 1;

> int CB_OTHER_INT = CB_NONE_INT + 1;

what's with that?! why not make them 0,1,2,3,4,5...

SoulTech2012a at 2007-7-14 22:40:06 > top of Java-index,Core,Core APIs...