Hi,
I am using a method:
Object lookup(Class c) {...}
The lookup method return an instance of the class c.
Is there is any way using java generic methods to insure compile-time
type checking? Something like:
T lookup(Class <T?????>)
Thanks.
Ross Bamford - 19 Oct 2005 13:11 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> Thanks.
I assume you mean something like:
public <T> T lookup(Class<? extends T> clazz)
throws IllegalAccessException, InstantiationException {
return clazz.newInstance();
}
P.s. compile time type safety is pretty much _all_ generics are good for ;)

Signature
Ross Bamford - rosco@roscopeco.remove.co.uk
tiboweb@btcweb.com - 19 Oct 2005 15:09 GMT
OK, that's working. Thanks all.
Ingo R. Homann - 19 Oct 2005 13:12 GMT
Hi,
> Hi,
>
[quoted text clipped - 6 lines]
> type checking? Something like:
> T lookup(Class <T?????>)
Yes, exactly:
T lookup(Class<T> c) {...}
Ciao,
Ingo
Roedy Green - 19 Oct 2005 14:20 GMT
>Is there is any way using java generic methods to insure compile-time
>type checking? Something like:
> T lookup(Class <T?????>)
see http://mindprod.com/jgloss/generics.html#FACTORIES

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.