Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For string constants its usual to use a class with final String values. But whats the best practice for storing string array. I want to store different categories in a constant array and everytime a category has been selected, I want to know which category it belongs to and process based on that.

Addition : To make it more clear, I have a categories A,B,C,D,E which is a constant array. Whenever a user clicks one of the items(button will have those texts) I should know which item was clicked and do processing on that.

I can define an enum(say cat) and everytime do

if clickedItem == cat.A
else if clickedItem = cat.B
else if 

or even register listeners for each item seperately.

But I wanted to know the best practice for doing handling these kind of problems.

share|improve this question
up vote 4 down vote accepted

I much prefer using Enums to store system wide string constants, makes it easy to create additional functionality and I think it is generally accepted best practice.

public enum Cat {
     A() {
          @Override public void execute() { 
               System.out.println("A clicked"); 
     B() {
          @Override public void execute() { 
               System.out.println("B Clicked"); 
     //template method
     public abstract void execute();

then you can call it like so :

String clickedItem = "A";    
Cat.valueOf(clickedItem).execute(); //will print A is clicked

or you could also the Command pattern without enums....

public interface Cat {
     void do();

public class CatA() implements Cat {
     void do() {//put your code here}
public class CatB() implements Cat {
     void do() {//put your other code here}

then build a Map object and populate it with Cat instances:

catMap.put("A", new CatA());
catMap.put("B", new CatB());

then you can replace your if/else if chain with:

share|improve this answer

Your description is quite vague, so maybe I got this wrong, but it sounds as you could use a Map<String,String> storing your category relationships. E.g. if you have

class Foo {
   final static String[] cat1 = {"val1_1","val1_2","val1_3"};
   final static String[] cat2 = {"val2_1","val2_2"};

I would write:

class Foo {
  final static Map<String, String> map = new HashMap<String,String>();

Then getting the category is as easy as calling"val1_1");. Of course you should be a little bit more clever than me filling when filling the map (e.g. from a file or so). If you are concerned with changes of the map, wrap it using Collections.unmodifiableMap.

share|improve this answer
To guarantee that the values aren't modified, you could wrap the map with Collections.unmodifiableMap(). – Mike Baranczak Nov 15 '11 at 16:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.