1

I'm a intermediate java programmer who has run into a conundrum.

I'm trying to decide what strategy to use when using fixed in-parameters to certain methods.

e.g. I want to do this:

void aMethod (int i, Color color)

where Color is my own creation.

They way I see it, I can either create an enum Color. I know how to do that.

I can also do this:

public class Color{

public static Color blue = new Color(23, 22, 21);
public static Color red = new Color(12, 32 ,10);

private int a;
private int b;
private int c;

private Color(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}

methods...

The thing is, that after trying both I can do exactly the same thing with them. The only difference is that I feel that my class example is more easily understood, I know exactly what it does. Even though I can write a multifunctional enum, I'm not absolutely sure what variables are static, public, private, etc, nor what kind of memory it will use. It's a little messy.

So, my question to you is, should I go with classes like the above, instead of enums? Are there some things I don't know that might add to Enums' case?

Jake
  • 843
  • 1
  • 7
  • 18
  • You can iterate and switch over enums easily - but not over static constants. – Stefan Neubert Nov 16 '14 at 16:49
  • possible duplicate of [What's the advantage of a Java enum versus a class with public static final fields?](http://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel) – Stanislav Lukyanov Nov 16 '14 at 18:20

3 Answers3

1

In your method

void aMethod (int i, Color color)

if you use the enum as your parameters. You can only use the color that you defined in the enums. However, if you use the static instances, you can't ensure the parameter is the instance color in your Color class. Actually, any variables which are Color can be transformed into this method.

And the enum is integer in the memory actually, use enum could save some time spent I think.

we1559
  • 26
  • 1
1

Use Enum when:

  • Pre-defined list of values (Objects)
  • Can't use a value that is not listed in the pre-defined list of values ( Can be validated - Safe type)
  • JVM only create fixed number of Object instances for Enum values.

Use Static when:

  • List of values (objects) is can be used is not Fixed
  • Considered as not safe method because the method parameter can be passed by any instance of the class.

Your case:

  • If you want to LIMITED / Validate Color that Caller can pass to 'aMethod' function -- Use ENUM
  • If you want Caller can pass ANY Color instance to 'aMethod' --- Use Static definition
LHA
  • 9,398
  • 8
  • 46
  • 85
0

If possible (you have limited values and do not want to allow dynamically created values) I would always go for an enum.

  • You effectively prevent bad input (see https://stackoverflow.com/a/9254703/1606867)
  • Enumy provide several additional functionalities:
    • You can iterate over an enum:
      for(ColorEnum enumEntry : ColorEnum.values()) //do something with enumEntry
    • You can use enums in switch/cases:
      ColorEnum ce; // with some value of course switch (ce) { case blue: break; case red: break; default: break; }
    • You can use enums for e.g. combo boxes, lists, etc. in GUI:
      new JComboBox<>(ColorEnum.values());
Community
  • 1
  • 1
Stefan Neubert
  • 1,053
  • 8
  • 22