15

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;
      }
   }
}
Philipp
  • 4,645
  • 3
  • 47
  • 80
Mathew
  • 153
  • 1
  • 1
  • 4
  • 2
    on a sidenote, according to java conventions enums should start with an upper case letter. – Dragondraikk Aug 06 '15 at 07:36
  • 1
    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). – Chop Aug 06 '15 at 07:37

4 Answers4

16

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);
    }
sonique
  • 4,539
  • 2
  • 30
  • 39
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Also, you could perhaps use a `switch` statement. – Xr. Aug 06 '15 at 07:38
  • Personally, I tend to avoid `switch` statements, in order not to break the [Open/Closed principle](https://en.wikipedia.org/wiki/Open/closed_principle). – Konstantin Yovkov Aug 06 '15 at 07:56
  • That is indeed a matter of personal opinion. I was merely mentioning the possibility :) – Xr. Aug 06 '15 at 08:36
  • in JVM switch statements are optimized to be far faster than if statements: https://stackoverflow.com/a/6705977/557153 – avgvstvs Jul 16 '17 at 01:52
  • @Konstantin Yovkov that might be a good reason, but then I ask myself, why exits the option to use `switch` statements? – karlihnos Feb 01 '19 at 09:16
  • @KonstantinYovkov How does `switch` statement break Open/Closed principle? – Alexandru Severin Feb 22 '21 at 12:17
  • @AlexandruSeverin, https://dzone.com/articles/the-power-of-openclosed-principle – Konstantin Yovkov Feb 22 '21 at 13:47
  • @KonstantinYovkov Thank you for the article. But using many `if`s instead of a switch statement doesn't help you in any way, you're still breaking O/C principle and even in a more inefficient way. The idea behind the article was to replace logic from switch statements with abstraction, which would allow you to extend implementations without touching the original code. However this would add extra complexity and the article even points out that its important to know when to use the principle and when not to. Trying to use it every time will only result in code hard to read and maintain – Alexandru Severin Feb 23 '21 at 08:23
  • @AlexandruSeverin, I have to disagree. It improves (a lot!) readability and testability. In my experience I've applied it many times and it doesn't bring extra complexity, but actually it's the other way around - code become simpler and maintainable. – Konstantin Yovkov Feb 23 '21 at 10:42
5

I think you should do something like these, an enum class. Then you can add as many types you 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;
    }
}
thebenedict
  • 2,539
  • 3
  • 20
  • 29
Eduardo
  • 73
  • 1
  • 6
1

Change your code to:

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

   public static decizion yourDecizion(){
      //scanner etc
      if(x.equals('Y')){
         return decizion.YES;
      }
      else if (x.equals('N')){
         return decizion.NO;
      }
      else{
         return decizion.OTHER;
      }
   }
}

Note: The method return type must be decizion instead of enum and decizion should have an upper case name (as all classes should).

Axel
  • 13,939
  • 5
  • 50
  • 79
1

You can get the value in below way. Here you have private constructor which will initialize the value you want to set and when the instance method value gets invoked simply return this.key.

public class Application {
    enum Day {
        MONDAY("Monday"), TUESDAY("Tuesday");

        String key;

        Day(String str) {
            key = str;
        }

        public String value() {
            return this.key;
        }
    }

    public static void main(String[] args) {
        System.out.println(Day.MONDAY.value());
    }

}
Tushar Girase
  • 183
  • 4
  • 15