Q:

How to use class type passed as parameter to method to define local variables

I have a cache that compartmentalizes by namespace. I would like to use class type to determine which cache to use. The following method gives an idea of what I want to accomplish. I use the word cache loosely. I am more interested in correcting my design pattern so it works.
public static DObject getFromCache(String key,Class<T extends DObject> type) {
  MyCache cache = getWithName(type.getName());
  // ......
  type.class value = (type.class) cache.get(key);
  // ......
}

where DObject is a naming interface. How do I fix so that type.class value = (type.class) cache.get(key) works well? I know this would be a joke for Scala but I want to use Java.

A:

T value = type.cast(cache.get(key));
(You can declare variables of type T just fine, but type erasure means you can't cast to T. Luckily Class#cast is there to help.)

