Re: Generics and forName()
- From: Roedy Green <my_email_is_posted_on_my_website@xxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 13:43:03 GMT
On Tue, 31 Jan 2006 09:35:32 +0100, Jacob <jacob@xxxxxxxxx> wrote,
quoted or indirectly quoted someone who said :
>The following gives an unchecked warning in the first line due to the
>forced cast. Using "?" just postpone the the problem:
>
> Class<A> clazz = (Class<A>) Class.forName("A");
> Constructor<A> ctor = clazz.getConstructor(String.class);
> A a = ctor.newInstance("hello world");
Unchecked means the compiler cannot guarantee safety at compile time.
The truth is it can't. It has absolutely no idea what A will be/do. If
you want the compiler to be able to check, it wants to see the code at
compile time.
Further this code makes no sense to me. If type A is known at compile
time, why are you dicking around with Class.forName. You only do that
when you DON'T know the class name, just an interface it implements..
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
.
- References:
- Generics and forName()
- From: Jacob
- Generics and forName()
- Prev by Date: Re: client-server connections
- Next by Date: Re: platform's default charset ?
- Previous by thread: Re: Generics and forName()
- Next by thread: Re: Generics and forName()
- Index(es):