Question: I have an enum representing all column values in a specific table. Now I want to store a type per enum element which is queried for later casting.

```
public enum Value {

    MEMBER1(?...?),
    MEMBER2(?...?);

    private final ?...? type;

    Value(?...? type) {
        this.type = type;
    }

    public ?...? getType() {
        return type;
    }

}

```

I don't now what I have to put instead of `?...?`. Maybe its `MyClassA.class` and `Class` but how can I use this value for this operation:

```
MEMBER1.getType() instance = (MEMBER1.getType()) object;

```

This is really important for database communication with java, for me.

**UPDATE: I mean this operation.**

END UPDATE: There was is logic error. If I knew the different types I can simply create multiple methods because I have a api specification.

Answer: 
You could do it by storing the Class of the object and use it to cast when necessary:

```
class Test {

    public static void main(String[] arguments) throws Exception {
        Object object = new MyClassA();
        Class<MyClassA> clazz = Value.MEMBER1.getType();
        MyClassA instance = clazz.cast(object);
        System.out.println(instance);
    }

}

enum Value {

    MEMBER1(MyClassA.class),
    MEMBER2(MyClassB.class);

    private final Class<?> clazz;

    Value(Class<?> clazz) {
        this.clazz = clazz;
    }

    @SuppressWarnings("unchecked")
    public <T> Class<T> getType() {
        return (Class<T>) clazz;
    }

}

class MyClassA { }
class MyClassB { }

```
> 
> how can I use this value for this operation:
> 
> 
> 

```
MyClassA instance = (MEMBER1.getType()) object;

```

Use **instanceof** to check object type.

```
MyClassA instance;
if (object instanceof MyClassA && object instanceof MEMBER1.getType()){ 
 instance = (MyClassA) object;
}

```