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 are right. The enum should be defined as follows

```
public enum Value {

   MEMBER1(String.class),
   MEMBER2(Integer.class);

   private final Class<?> type;
   private Value(Class<?> type) {
       this.type = type;
   }

   public Class<?> getType(){
       return this.type;
   }
}

```

The second part is actually something you don't want to do because you already know what is the type you want to cast your object to (In your case is it MyClassA) so you can cast directly like following

```
//I know what is the target type so I don't have to fetch the type from my enum
MyClassA instance = (MyClassA) object;

```

Once you will need to build your logic on unknown data type, than you can do something like

```
Object o = getTheObjectSomewhere();
if(o.getClass().isAssignableFrom(MEMBER1.getType()){
    String s = (String) o;
    System.out.println("Target type is string!");
    doSomething();
} else if (o.getClass().isAssignableFrom(MEMBER2.getType()){
    Integer i = (Integer) i;
    System.out.println("Target type is integer!");
    doSomethingElese();
}

```

Maybe you want to search the column later according to the type of your object. Then you can create a method to get the column

```
public Value getColumnByObjectType(Object o){
    for(Value v : Value.values()){
        if(v.getType().equals(o.getClass()){
            return v;
        }
    }
    return null;
}

```

And use it to search for the column

```
Object o = getObjectSomewhere();
Value column = getColumnByObjectType(o);

```

To get the user object according to the comment bellow you can do something like following using JPA

```
public Object getUserObject(int id, Value v){
    TypedQuery<?> q = DBUtils.getEntityManager().createQuery("select u.:column from User u where u.id = :id",v.getType());
    q.setParameter("column", v.toString());
    q.setParameter("id", id);
    q.setMaxResult(1);
    return q.getSingleResult();
}

```
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 { }

```