0

After doing some research, I see since java 1.5 it is preferable to manage constants (or key properties) in enum which are declared in a "constant" class.

I'd like to know what is the proper way to implement these enum to get as simple as possible the value of an element of an enum whatever its type.

Example : is it possible to have the value of "Constant.Messages.WARNING" where Messages is :

public enum Messages {

    WARNING("main.message.warning")

}

Thank you.

MychaL
  • 969
  • 4
  • 19
  • 37
  • 2
    Any sources for *why* `enum` is preferred over just having `static final`s? I do see the benefit for a *fixed, finite* set such as `NORTH,EAST,SOUTH,WEST` so you cannot set the value to a fifth. But for e.g. warning messages? – Has QUIT--Anony-Mousse Jan 09 '13 at 17:30
  • See the second half of this page http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – Peter Lawrey Jan 09 '13 at 17:30
  • `enum`s are preferable when you have a fixed number of names instances of a class. e.g. a Singleton. It is cleaner and gives more functionality. – Peter Lawrey Jan 09 '13 at 17:31
  • I see this example which is interesting. There is an answer to explain why enum can be a good practice [http://stackoverflow.com/questions/1858829/static-string-constants-vs-enum-in-java-5](http://stackoverflow.com/questions/1858829/static-string-constants-vs-enum-in-java-5) – MychaL Jan 10 '13 at 15:44

5 Answers5

3

You can do it by implementing a few methods. You will need a constructor and a getter:

public enum Messages {
    WARNING("main.message.warning");

    private String message;

    Messages (String message) { this.message = message; }

    public String get() { return this.message; }
}

Now you can write:

String msg = Messages.WARNING.get();
assylias
  • 321,522
  • 82
  • 660
  • 783
3

You can customize enums as other classes in Java with few exceptions.

I'll use the same example, but it uses toString without getMessage, so you can use enums in those places where toString is called (for example, in System.out.println or any logging methods).

public enum Messages
{
    WARNING("main.message.warning"),
    ERROR("main.message.error");

    private final String message;

    // Parameters to the enum constructors are the ones in the enum "definition"
    Messages(final string message)
    {
        this.message = message;
    }

    @Override
    public String toString()
    {
        return message;
    }
}
tcb
  • 2,745
  • 21
  • 20
2
public enum Messages {

    WARNING("main.message.warning")
    private String message;
    Messages(String message) {
        this.message = message
    }
    String getMessage() {
        return this.message;
    }
}

and afterall you can

Messages.WARNING.getMessage()
Alexey Sviridov
  • 3,360
  • 28
  • 33
2

Yes it is, but in this case this "main.message.warning" is an instance member of the enum:

public enum Messages
{
    WARNING("main.message.warning"),
    ERROR("main.message.error");

    private final String message;

    // Parameters to the enum constructors are the ones in the enum "definition"
    Messages(final string message)
    {
        this.message = message;
    }

    public String getMessage()
    {
        return message;
    }
}

Note also that all of these constructors are called at JVM startup time; when your program is "ready to go", the enum values are all there.

fge
  • 119,121
  • 33
  • 254
  • 329
0

The Java compiler does some magic behind the scenes, so enums feel a bit odd when you encounter them. You can add arbitrary additional data using a private constructor, for example:

public enum Messages {

    WARNING("main.message.warning");

    private final String key;

    private Messages(String key) { this.key = key; }
    public String getKey() { return key; }
}
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820