Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating an enum/final class in java

I'm trying to figure out the best way to create a class whose sole purpose is to be a container for global static variables. Here's some pseudocode for a simple example of what I mean...

public class Symbols {
   public static final String ALPHA = "alpha";
   public static final String BETA = "beta";

   /* ...and so on for a bunch of these */
}

I don't need constructors or methods. I just need to be able to access these "symbols" from everywhere simply by calling: Symbols.ALPHA;

I DO need the actual value of the String, so I can't use an enum type. What would be the best way to accomplish this?

like image 327
Hristo Avatar asked Jul 14 '11 17:07

Hristo


People also ask

Can you make enum final in Java?

Java does not allow an enum to be final inside an interface, but by default every data member inside an interface is public static final .

Can you create enum in a class in Java?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Can enums have final?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).

How do you create an enum in Java?

How to Create an Enum in Java. To create an enum , we use the enum keyword, similar to how you'd create a class using the class keyword. In the code above, we created an enum called Colors . You may notice that the values of this enum are all written in uppercase – this is just a general convention.


2 Answers

Well, it's not clear what else you need beyond the code you've already given - other than maybe making the class final and giving it a private constructor.

However, in order to avoid accidentally using an inappropriate value, I suspect you would be better off making this an enum, like this:

public enum Symbol {
   ALPHA("alpha"),
   BETA("beta");

   private final String value;

   private Symbol(String value) {
     this.value = value;
   }

   public String getValue() {
     return value;
   }
}

That way:

  • You can't accidentally use Symbol.ALPHA where you're really just expecting a string
  • You can't accidentally use a string where you're really expecting a symbol
  • You can still easily get the string value associated with a symbol
  • You can switch on the different symbol values if you need to
like image 84
Jon Skeet Avatar answered Oct 07 '22 02:10

Jon Skeet


You can do that using an interface. No need to construct, values are public, static and final, and can obviously be strings. Such an interface would look similar to your class:

public interface Symbols {
  public static final String ALPHA = "alpha";
  public static final String BETA = "beta";
  /* and so on */
}

You can access the fields directly from everywhere in your code (given it's public) as Symbols.ALPHA etc.

Or, you can use an enum even though you want strings - ALPHA.toString() will return "ALPHA" (and if you want a slightly different string, you can override toString())

like image 26
eran Avatar answered Oct 07 '22 02:10

eran