1

I have a class that has hundreds of static fields int fields represents colors.

public class LColorPallette {
      public static final int RED_050 = 0xfde0dc;
      ...
}

I want to have them in a container that can be arraylist, map or set. I know a static namespace can be declared so that there would be something like that

public class ColorPalletteSingleton {
      static {
            container.add(...);
            ...
}

I need to an example to how to do it or any others ways do solve my problem?

Thanks

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30

4 Answers4

3

static {} is not a "static namespace", it's a static initializer block, use to initialize static variables.

You can store your colors in a static Collection.

For example :

public static List<Integer> colors = new ArrayList<>;

static {
    colors.add (RED_050);
    ...
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Do you have any other idea to solve problem? For example what about reflection? And can you please tell what is namespace? Where to use it? :) – Emre Aktürk Nov 23 '15 at 09:15
  • @EmreAktürk I'm not aware of the term `namespace` as far as Java is concerned. Java packages have some things in common with C# namespaces. I don't see why you'd want to use reflection here. – Eran Nov 23 '15 at 09:26
  • Its a sample project to experince some cases to learn. I could implement reflection to learn it :) Otherwise i know its a bad choice since its effect performance and proguard. – Emre Aktürk Nov 23 '15 at 09:31
  • @Eran reflection lets you loop through the variables, so you don't have to have many lines of colors.add(...) and etc – nafas Nov 23 '15 at 09:31
2

Instead of static fields prefer enum.

What's the advantage of a Java enum versus a class with public static final fields?

 enum LColorPallette {
    RED_050(0xfde0dc);
    private int hexValue;

    private LColorPallette(int hexValue) {
        this.hexValue = hexValue;
    }

    public String getHexValue()
    {
        return Integer.toHexString(hexValue);
    }

}

Enum has values method which will return array.No need to loop and add into arrayList

 List<LColorPallette> somethingList = Arrays.asList(LColorPallette .values());

UPDATE : As VBR recommended Prefer EnumSet instead of List

EnumSet set = EnumSet.allOf(LColorPallette .class);
Community
  • 1
  • 1
rupesh_padhye
  • 1,355
  • 2
  • 13
  • 25
  • Too bad that i have to right so much boiler plate code for implementing enum. I have 200+ fields :( – Emre Aktürk Nov 23 '15 at 09:33
  • any how you are going to write 200+fields even if you choose to write static final fields – rupesh_padhye Nov 23 '15 at 09:34
  • This is much preffered clean way to write it .if you really want to make it dynamic and loading some place probably database ..then Class make sense and obviously you will use JPA – rupesh_padhye Nov 23 '15 at 09:37
  • Better than using Arrays.asList would be `EnumSet.allOf(LColorPallette.class)`. – VGR Nov 25 '15 at 01:30
  • @VGR Yes,we can use EnumSet but I don't find any strong reason to do so(even on SO).let us know if you have – rupesh_padhye Nov 25 '15 at 07:27
  • @rupesh_padhye EnumSet won't need a reference to each and every constant. Internally, it uses a bit mask, one bit for each constant's ordinal value. This also means membership tests require constant time. For large enums, EnumSet can mean a noticeable difference in performance. – VGR Nov 25 '15 at 19:25
2

you can try using reflection to get all the fields.

    List<Integer> result = new ArrayList<Integer>();
    Field[] fields = LColorPallette.class.getDeclaredFields();
    for(Field classField : fields){
        result.add(classField.getInt(classField.getName()));    
    }
    System.out.println(result);
nafas
  • 5,283
  • 3
  • 29
  • 57
1

As a Map:

public static final int RED_050 = 0xfde0dc;
public static final int RED_051 = 0xfde0df;

public void test() throws IllegalArgumentException, IllegalAccessException {
    Map<String, Integer> colours = new HashMap<>();
    Class<Test> myClass = (Class<Test>) this.getClass();
    Field[] fields = myClass.getDeclaredFields();
    for (Field field : fields) {
        Type type = field.getGenericType();
        // TODO: Make sure it is an int
        int value = field.getInt(field);
        System.out.println("Field " + field.getName() + " type:" + type + " value:" + Integer.toHexString(value));
        colours.put(field.getName(), value);
    }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213