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.

