Why generics for the Class type?

Discussion in 'Java' started by Josef Garvi, Apr 28, 2005.

  1. Josef Garvi

    Josef Garvi Guest

    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
     
    Josef Garvi, Apr 28, 2005
    #1
    1. Advertising

  2. 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
     
    John C. Bollinger, Apr 28, 2005
    #2
    1. Advertising

  3. Josef Garvi

    Josef Garvi Guest

    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
     
    Josef Garvi, Apr 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    6,033
    John C. Bollinger
    May 20, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  4. Soul
    Replies:
    0
    Views:
    535
  5. Mayeul
    Replies:
    2
    Views:
    3,427
Loading...

Share This Page