-2

I've got 2 enum classes:

public enum Dogs
{
 JACKIE("Jackie"),
 BOBBY("Bobby");
}

and

public enum Cats
    {
     CHAN("Chan"),
     SPONGE("Sponge");
    }

And now I have a method in some other class where I want to pass enum as a parameter, so I could select either of them as per my needs.

public void findAnimal()
{
    List<Dogs> animals = Arrays.asList(Dogs.values());
    for (Dogs animal: animals)
    {
        rest of the code goes here
    }
}

So instead of providing hardcoded 'Dogs' I want to have a choice and pass parameter to the method, something like

public void (Enum enumAnimals)
{
    List<Enum> animals = Arrays.asList(Enum.values());
    for (Enum animal: animals)
}

But it doesnt accept it like this. Is it possible to pass generic type as "Enum'?

rooddah
  • 21
  • 6
  • Your enums can implement a common interface. What do you want to do inside the for loop? – Klitos Kyriacou Mar 13 '19 at 12:06
  • Possible duplicate of [How to implement enum with generics?](https://stackoverflow.com/questions/11490485/how-to-implement-enum-with-generics) – nortontgueno Mar 13 '19 at 12:08
  • This seems like a fundamental misuse of enums. I'd expect each of Chan, Sponge, etc. to be objects of a class, not enum members. – Ben R. Mar 13 '19 at 12:40
  • @BenR. R. Maybe I provided wrong examples of values, but I need the String values and not objects of a class. Those names will not change and will be used in many places in the application/code. That's why I need them to be constant. – rooddah Mar 14 '19 at 09:26
  • @KlitosKyriacou in 'for loop' I will do something like this: input.typeName(animal.getName()) if(some condition) { String newName = animal.getName(); break; } – rooddah Mar 14 '19 at 09:28

3 Answers3

5

Enum is the base class for all enumerations so Enum.values() won't do anything. If you want to use multiple enums you could have them implement a common interface but even then you'd need to provide the instances. This could be done via reflection but I feel handing you that power now would be a receipe for disaster. In general enums are meant for static types like AnimalType and not for individual instances like a specific cat.

I'd suggest you have something like this (very simplified code to make it short):

enum AnimalType { 
  CAT,
  DOG;
}

class Animal {
  String name;
  AnimalType type;

  Animal( String n, AnimalType t) {
    name = n;
    type = t;
  }
}

Set<Animal> animals = ... //create a set
animals.add( new Animal( "Jackie", AnimalType.CAT ) );
animals.add( new Animal( "Chan", AnimalType.DOG) );
...

Note that many examples/tutorials that use a class like Animal focus more on inheritance, i.e. class Cat extends Animal etc. Reading between the lines of your question makes me feel that this is what you're actually after, not how to use enums.

Thomas
  • 87,414
  • 12
  • 119
  • 157
1

What you tried doesn't work largely because Enum, itself, doesn't have a values method. That's an implicit static method of the enum classes.

If you absolutely have to do this with separate Dogs and Cats enum types rather than doing it the way Thomas suggests, you could pass in the result of calling values:

obj.findAnimal(Dogs.values());
// or
obj.findAnimal(Cats.values());

then the method would be simply:

public void findAnimal(Enum[] animals) {
    for (Enum animal : animals) {
        System.out.println(animal);
    }
}

If that isn't suitable for some reason, it's also possible using Class.getEnumConstants: Rather than passing in Dogs or Cats, you'd pass in their class:

obj.findAnimal(Dogs.class);
// or
obj.findAnimal(Cats.class);

The method would be along these lines:

public void findAnimal(Class enumClass)
{
    Enum[] animals = (Enum[])enumClass.getEnumConstants();
    for (Enum animal: animals) {
        System.out.println(animal);
    }
}

(Thank you Pshemo for pointing out that my earlier version could be simplified markedly using getEnumConstants.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Not quite sure exactly what you want to do in this method but you can do something like this.

enum Dogs {
    JACKIE("Jackie"),
    BOBBY("Bobby");
    private final String name;

    Dogs(String name) {
        this.name = name;
    }
}

enum Cats {
    CHAN("Chan"),
    SPONGE("Sponge");
    private final String name;

    Cats(String name) {
        this.name = name;
    }
}

<E extends Enum<E>> void method(E e) {
    Enum<E>[] animals = e.getClass().getEnumConstants();
    for (Enum<E> animal : animals) {
        System.out.println(animal);
    }
}

private void test() {
    method(Dogs.BOBBY);
    method(Cats.CHAN);
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213