• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class and Generics ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if I only have a Class, how to use generics ?

Like this....


But this can't pass compiler, how to work out ?

Thanks
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Generics uses Type information not the names of instances.
Type information is the name of a class, suct as String or 2DPoint.

Therefore in your example you should replace myClass with Class, as Class is the type of the objects you plan to store in the list.

hope this helps
Gavin
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gavin Tranter wrote:
Therefore in your example you should replace myClass with Class, as Class is the type of the objects you plan to store in the list.



Are you saying

List<Class> peopleList = (List<Class>)anotherList;

But this can't help me. My goal is to dynamically cast a list to another list based on its Class type.

The perfect solution for me will be something like this



How to work out ?

Thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics are a compile-time only construct. At runtime, a List contains only Objects; therefore your request really doesn't make any sense; is it not necessary.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't exactly what you asked for, but you might like Collections.checkedList(). It's not "casting" , but it creates a list that can only contain the specified type. Unless the list already contained some other type within it, in which case you'll get a ClassCastException if you try to access it.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic