Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why generics for the Class type?

Reply
Thread Tools

Why generics for the Class type?

 
 
Josef Garvi
Guest
Posts: n/a
 
      04-28-2005
Why does the Class type use Generics?

Or in other words - how am I supposed to use it?
I store certain Classes in hash tables, and then create dynamic instances
of objects according to the correct type at run-time. What "type of class"
should my Class variables be??

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      04-28-2005
Josef Garvi wrote:

> Why does the Class type use Generics?
>
> Or in other words - how am I supposed to use it?
> I store certain Classes in hash tables, and then create dynamic
> instances of objects according to the correct type at run-time. What
> "type of class" should my Class variables be??


It depends, but some way around you're going to need to use a wildcard
type parameter. Class<?> will definitely work, but you may be able to
use something more specific. For instance, if you are storing only
Class objects for classes that implement Serializable, you could use
Class<? extends Serializable>. You may indeed want to do this sort of
thing to achieve type safety and dispense with casting when you create
instances:

Class<? extends Serializable> clazz;

[...]

Serializable ser = clazz.newInstance(); /* typesafe; no cast required */


--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Josef Garvi
Guest
Posts: n/a
 
      04-28-2005
John C. Bollinger wrote:

> It depends, but some way around you're going to need to use a wildcard
> type parameter. Class<?> will definitely work, but you may be able to
> use something more specific. For instance, if you are storing only
> Class objects for classes that implement Serializable, you could use
> Class<? extends Serializable>. You may indeed want to do this sort of
> thing to achieve type safety and dispense with casting when you create
> instances:
>
> Class<? extends Serializable> clazz;
>
> [...]
>
> Serializable ser = clazz.newInstance(); /* typesafe; no cast required */


Thanks. That's how I wanted it to work!

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Generics: SupressWarnings("unchecked"). Why, and why not in java.util.ArrayList Mayeul Java 2 10-21-2009 07:52 PM
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments