43

I want to convert this sample C# code into a java code:

public enum myEnum {
  ONE = "one",
  TWO = "two",
}; 

Because I want to change this constant class into enum

public final class TestConstants {
    public static String ONE = "one";
    public static String TWO= "two";
}
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
newbie
  • 14,582
  • 31
  • 104
  • 146
  • 7
    http://download.oracle.com/javase/tutorial/java/javaOO/enum.html – Scott Aug 10 '11 at 07:18
  • As for enums, there're many other correct answers below by now (and a must read link above) . Just wanted to add that if your class holds some constants like in your example (I am not talking about enums right now!!!) you can simply define interface this way: public interface TestConstants { String TEST = "test"; /*...*/ } Interface 'fields' are implicitly public static. – Art Licis Aug 10 '11 at 07:27
  • 1
    @Arturs Licis: Using an interface to hold constants is an anti-pattern. An interface is meant to define behavior. Use a regular class with a private constructor to hold constants. Static imports can be used to access the constants without putting the class name before. – JB Nizet Aug 10 '11 at 07:34
  • @JB Nizet: Completely agree, that's why I said 'I am not talking about enums right now!!!' I just showed a more accurate way to declare 'constants holder' (interface versus class). – Art Licis Aug 10 '11 at 08:01
  • @Arturs Licis: My point is that you shouldn't use an interface to hold constants, but a class: public class TestConstants rather than public interface TestConstants. If you completely agree with me, why do you give this advice to the OP? – JB Nizet Aug 10 '11 at 08:18
  • @JB Nizet My bad, I was into some other task at work, and scanned through your comment too hastily, so I thought you've mentioned that using enum as constant holders is a bad idea. Please excuse my inattention. However, I don't consider this to be a real anti-pattern. I understand the purpose of interface, but in this specific case I don't see anything bad (that could lead to mistakes) - you don't have to implement iface to use those constants. P.S. static imports can be applied to interface as well. – Art Licis Aug 10 '11 at 09:44
  • What If I want to do the quivalnet of setting a value of one of the enums to a combination of other enums? for instance in C# All = this| that| other – topwik Jul 26 '12 at 16:43

6 Answers6

89
public enum MyEnum {
   ONE(1),
   TWO(2);
   private int value;
   private MyEnum(int value) {
      this.value = value;
   }
   public int getValue() {
      return value;
   }
}

In short - you can define any number of parameters for the enum as long as you provide constructor arguments (and set the values to the respective fields)

As Scott noted - the official enum documentation gives you the answer. Always start from the official documentation of language features and constructs.

Update: For strings the only difference is that your constructor argument is String, and you declare enums with TEST("test")

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
14

enums are classes in Java. They have an implicit ordinal value, starting at 0. If you want to store an additional field, then you do it like for any other class:

public enum MyEnum {

    ONE(1),
    TWO(2);

    private final int value;

    private MyEnum(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
9

Quite simply as follows:

/**
 * @author The Elite Gentleman
 *
 */
public enum MyEnum {
    ONE("one"), TWO("two")
    ;
    private final String value;

    private MyEnum(final String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return getValue();
    }
}

For more info, visit Enum Types from Oracle Java Tutorials. Also, bear in mind that enums have private constructor.


Update, since you've updated your post, I've changed my value from an int to a String.
Related: Java String enum.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
5

Well, in java, you can also create a parameterized enum. Say you want to create a className enum, in which you need to store classCode as well as className, you can do that like this:

public enum ClassEnum {

    ONE(1, "One"), 
    TWO(2, "Two"),
    THREE(3, "Three"),
    FOUR(4, "Four"),
    FIVE(5, "Five")
    ;

    private int code;
    private String name;

    private ClassEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}
Jerry U
  • 618
  • 9
  • 22
ahmar hashmi
  • 99
  • 2
  • 7
4
public enum MyEnum
{
    ONE(1),
    TWO(2);

    private int value;

    private MyEnum(int val){
        value = val;
    }

    public int getValue(){
        return value;
    }
}
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • 1
    why the underscore? And the typical convention is to have the field, then the constructor, then the getter. – Bozho Aug 10 '11 at 07:25
2
public enum NewEnum {
   ONE("test"),
   TWO("test");

   private String s;

   private NewEnum(String s) {
      this.s = s);
   }

    public String getS() {
        return this.s;
    }
}
ssedano
  • 8,322
  • 9
  • 60
  • 98