Determining type accepted by a generic collection

B

bierbaum

Hello all,

Is it possible to determine the types accepted by a generic collection
that has been cast back to a Collection from an Object?

Thanks in advance :)
 
J

John C. Bollinger

Is it possible to determine the types accepted by a generic collection
that has been cast back to a Collection from an Object?

No. Java type parameterization operates entirely at compilation time,
and therefore depends crucially and exclusively on *declarations*. Once
you throw in a cast -- explicit or implicit -- you lose information that
you cannot regain. (Though it may be available from some other source.)

A technical description for this property of Java's type system might be
"generic types are not reifiable".
 
J

Jim McMaster

Hello all,

Is it possible to determine the types accepted by a generic collection
that has been cast back to a Collection from an Object?

Thanks in advance :)

No, it is not possible. The generic definitions are used at compile
time, then discarded. At run time, there is no record of the generic
type of the collection.

This is the big problem with the Java generic implementation. It does
*not* ensure type safety, but people think it does.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top