Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Avoid a cast when using class literals



Hello. I have a question on java type safety.

I have a method that accepts a class literal and (for sake of argument) returns an instance of any class (in fact there is slightly more to it).

Example:

Circle c = (Circle)this.returnInstance(Circle.class);
//removed exception handling

private Object returnInstance(Class c) throws Exception {
return c.newInstance();
}

My question is - is there any way to avoid performing the CAST (here, on the Circle instance) in the calling code?

Many thanks,
B