Creating an enum / final class in java

I am trying to find a better way to create a class whose sole purpose is a container of global static variables. Here's some pseudo code 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 have access to these "characters" everywhere, just by calling:Symbols.ALPHA;

I An actual String value is required , so I cannot use an enumeration type. What would be the best way to achieve this?

+5
source share
3 answers

, , , , - , , , .

, , , , :

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

   private final String value;

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

   public String getValue() {
     return value;
   }
}

:

  • Symbol.ALPHA,
  • ,
  • ,
  • ,
+10

. , , , , , . :

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

( ) Symbols.ALPHA ..

, , - ALPHA.toString() "ALPHA" ( , toString())

+4

"", ? . , . DI (Spring/Guice), / .

0

All Articles