0

I'm trying to retrieving a value depending on the enum value. Basically, let's say I have the following enum:

    private enum Auth{

    KEY, PASSWORD, MAIL;    

    public String get(){
        return "";
    }
}

By doing Auth.KEY.get() it would return "mykey", while Auth.MAIL.get() would return "mymail" I googled a bit but I couldn't find an answer, I didn't try anything before because I totally hadn't an idea on how I could start.

m7913d
  • 10,244
  • 7
  • 28
  • 56
  • 1
    Just google for "Java enum tutorial", click on the first link, and read. – JB Nizet Mar 11 '17 at 09:11
  • enums can have a constructor and its own fields.... consider that too please – ΦXocę 웃 Пepeúpa ツ Mar 11 '17 at 09:12
  • I would like to do this without a constructor –  Mar 11 '17 at 09:13
  • Possible duplicate of [Can I add a function to enums in Java?](http://stackoverflow.com/questions/2457076/can-i-add-a-function-to-enums-in-java) – m7913d Mar 11 '17 at 09:13
  • @RubenOktay you can do it with an abstract method overridden in each enum, or your get() method could use a switch statement to test the value of `this`, but a field and a constructor is way simpler. Why wouldn't you use that? – JB Nizet Mar 11 '17 at 09:19
  • Well, i cannot use a constructor as the string will be updated often.. –  Mar 11 '17 at 09:23
  • 1
    That's probably a bad idea, but anyway, I don't see how that prevents you from using a constructor. To the contrary. If the string s updated, you *need* a field to contain the new value, and you thus need a constructor to initialize the field. – JB Nizet Mar 11 '17 at 09:25

4 Answers4

1

Just add a field and constructor, as explained in the java-docs

Example code:

enum Auth {

    KEY("myKey"), PASSWORD("myPass"), MAIL("myMail");    

    private final String identifier;

    Auth(String identifier) {
        this.identifier = identifier;
    }

    public String get(){
        return identifier;
    }
}

Also note, that there is name() and toString() which may be useful: see also java-enum-why-use-tostring-instead-of-name

Community
  • 1
  • 1
TmTron
  • 17,012
  • 10
  • 94
  • 142
0
enum Auth{
private enum Auth{
String value;
KEY("mykey"), PASSWORD("mypassword"),    MAIL(mymail");    

Auth(String value){
thia.value=value;
}
public String get(){
    return value;
}
}
0

You need to have a string which holds the name of of enum and a constructor which sets it. Then get method returns the name as below.

 private enum Auth{

    KEY, PASSWORD, MAIL;    
    string name;
    public Auth(string nm) {
      name = nm;
    }
    public String get(){
        return name;
    }
 }
hmatar
  • 2,437
  • 2
  • 17
  • 27
0

Although I strongly advise not to use the following mechanism for getting something simple like a hardcoded String from an enum value and rather use it for associating particular behavior like in java.util.concurrent.TimeUnit, this is how it can be achieved:

private enum Auth {

    KEY {
        public String get() {
            return "mykey";
        } 
    },

    PASSWORD {
        public String get() {
            return "mypassword";
        } 
    },

    MAIL {
        public String get() {
            return "mymail";
        } 
    };

    public abstract String get();

}
Markus Benko
  • 1,507
  • 8
  • 8