0

I want to retrieve the name of the enum type from within the enum type itself:

enum Mammals {
    DOG(new Dog()),
    CAT(new Cat());

    public String alias;

    Mammals(AncestorOfDogAndCat a){
        this.alias=this.getClass().getName().toLowerCase();
        System.out.println(alias);
    }
}

When I instance them I get

Main$mammals
Main$mammals

but I want

dog
cat
3
  • you didn't pass parameter on constructor but i can see each enum constant has values. why? Commented Aug 3, 2017 at 14:11
  • @atiqkhaled sorry, I stripped some unnecessary code but left some garbage Commented Aug 3, 2017 at 14:13
  • 1
    I don't understand why you need to pass the Dog instance if you are not using nor storing it. There are several answers already relying on the name() method but if you need the instance coudn't you simply call to its toString method? That would make the print dependant to the dog/cat instance instead of the enum type name. Commented Aug 3, 2017 at 14:22

4 Answers 4

2

You can use the name() method on an enum constant to get the name of the enum constant:

enum Mammals {
    DOG(),
    CAT();

    public String alias;

    Mammals() {
        this.alias = name().toLowerCase();
        System.out.println(alias);
    }
}

The name() method is something that is automatically added by the compiler on enum types.

Sign up to request clarification or add additional context in comments.

2 Comments

From the documentation for name: "Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name. This method is designed primarily for use in specialized situations where correctness depends on getting the exact name, which will not vary from release to release."
@Michael which means the name() method is exactly what you should use if your program depends on it; what toString() returns is more intended for humans to read.
2

Don't use reflection. It's not robust enough. Provide a method which provides this information.

interface Animal
{
    String getName();
}

class Dog implements Animal
{
    public String getName()
    {
        return "dog";
    }
}

enum Mammals {
    DOG(new Dog()),
    CAT(new Cat());

    public String alias;

    Mammals (Animal animal) {
        this.alias = animal.getName();
        System.out.println(alias);
    }
}

7 Comments

Could you please elaborate why reflection isn't robust?
You're creating a link between the class name to run-time behaviour. Bad idea. What if you someone comes along later and refactors the Dog class and changes the name to Canine? Seems like a reasonable change - it will compile fine. Only days, weeks, months later will you realise you've broken something.
@Michael although I accept your concerns about reflection, if you are only looking for the name you can use the name method for enums instead of adding a new parameter with a "custom" string value. I mean, with your same argument, if someone refactors the class the enum is still showing the "dog" value and it should now be "canine"
@CristianRamon-Cortes No good programmer would reasonably expect refactoring the class name to Canine to change run-time behaviour to output 'Canine'.
@Michael we could discuss it further but I would expect something more like the solution you have edited now. I mean, if someone refactors the dog class he will refactor the getName or the toString method conveniently.
|
0
enum Mammals {
    DOG(new Dog()),
    CAT(new Cat());

    public String alias;

    Mammals(DogOrCat value){
        this.alias=name().toLowerCase();
        System.out.println(alias);
    }
}

or use toString(), which by default is implemented using name.

Comments

0

Using this.getClass() will return the name of the class itself, which is the enum Mammals here. Instead take in a parameter and use getClass on the parameter for your alias.

enum Mammals {
DOG(new Dog()),
CAT(new Cat());

public String alias;

  Mammals(Animal an){
      this.alias=an.getClass().getName().toLowerCase();
      System.out.println(alias);
  }
}

where Dog and Cat extend Animal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.