As of Java 8 you can have default or static methods implemented in Interfaces as the below
public interface DbValuesEnumIface>
There is no easy way as far as I can tell, first you need to change your method to default
, you can read more here of why you can't use generics in a static context.
But even if you change it to default
things are still not going to work, since you need to pass an instance or class type of the enum to that method, something like this:
public default T fromId(ID id, Class t) {
if (id == null) {
return null;
}
for (T en : t.getEnumConstants()) {
// dome something
}
return null;
}
Now you are hitting another problem, inside fromId
- the only thing that you know is that T
extends an enum
- not your enum may be, thus getId
(which seems that your enums have) are simply not known by the compiler.
I don't know an easy way to make this work besides declaring an interface, like :
interface IID {
public int getId();
}
making your enum
implement it:
static enum My implements IID {
A {
@Override
public int getId() {
// TODO Auto-generated method stub
return 0;
}
};
}
and change the declaration to:
public interface DbValuesEnumIface & IID>