jeudi 3 août 2017

Java enum type name reflection

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(){
        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





Aucun commentaire:

Enregistrer un commentaire