Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return enum value in java

Tags:

java

enums

return

How can I return enums like this?

Before I was returing an int with 0 if no, 1 if yes and 2 if other. But this wasn't good way to do. So how should it be done. My code:

class SomeClass{
   public enum decizion{
      YES, NO, OTHER
   }

   public static enum yourDecizion(){
      //scanner etc
      if(x.equals('Y')){
         return YES;
      }
      else if (x.equals('N')){
         return NO;
      }
      else{
         return OTHER;
      }
   }
}
like image 837
Mathew Avatar asked Aug 06 '15 07:08

Mathew


People also ask

Can I return enum in Java?

How can I return enums like this? on a sidenote, according to java conventions enums should start with an upper case letter. An enum is a (special type of) class, so you should declare it as the return type of your method. By the way, it would be better to name it Decision (it is a class).

What does enum value return?

valueOf() method returns the enum constant of the specified enumtype with the specified name. The name must match exactly an identifier used to declare an enum constant in this type.

How do you find the value of an enum?

To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

How do I return an enum from a function?

In C, you must use enum Foo until you provide a typedef for it. And then, when you refer to BAR , you do not use Foo. BAR but just BAR . All enumeration constants share the same namespace (the “ordinary identifiers” namespace, used by functions, variables, etc).


2 Answers

I don't what the "//scanner etc." does, but the methods return type should be decizion:

    public static decizion yourDecizion() { ... }

Furthermore, you can add the Y, N, etc. values to the enum constants:

    public enum decizion{
         YES("Y"), NO("N"), OTHER;
          
         String key;
      
         decizion(String key) { this.key = key; }
     
         //default constructor, used only for the OTHER case, 
         //because OTHER doesn't need a key to be associated with. 
         decizion() { }

         static decizion getValue(String x) {
             if ("Y".equals(x)) { return YES; }
             else if ("N".equals(x)) { return NO; }
             else if (x == null) { return OTHER; }
             else throw new IllegalArgumentException();
         }
    }

Then, in the method, you can just do:

    public static decizion yourDecizion() {
        ...
       String key = ...
       return decizion.getValue(key);
    }
like image 151
Konstantin Yovkov Avatar answered Sep 23 '22 16:09

Konstantin Yovkov


I think you should do something like these, an enum class. Then u can add as many types u want and the method yourDecizion() will return the enum type depending on the given parameter.

public enum SomeClass {

        YES(0),
        NO(1),
        OTHER(2);

    private int code;


    private SomeClass(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public static SomeClass yourDecizion(int x) {
        SomeClass ret = null;
        for (SomeClass type : SomeClass.values()) {
            if (type.getCode() == x)
                ret = type;
        }
        return ret;
    }
}
like image 26
Eduardo Avatar answered Sep 23 '22 16:09

Eduardo