java - why Class class need to be Generic? -
for example:
class<string> c = string.class; i think, class object of string may have method know, it's act on string type.
so, type param no need.
if you're asking why class generic, it's type-safe generic methods can accept class parameter provide type bound.
consider enumset. way create empty enumset of myenum use noneof:
enumset<myenum> set = enumset.noneof(myenum.class) since class generic, compiler knows static noneof method returning enum<myenum>. idiom used extensively in persistence , remoting apis, such jpa type-safe queries , spring data projections, provide class literal @ compile time, , compiler understands api returning instance of class.
an example in standard jre: legacy resultset#getobject(int columnindex) method returns object must cast. new resultset#getobject(int columnindex, class<t> type) tells jdbc driver want data in column converted specific class and compiler you'll getting type method, don't need cast.
Comments
Post a Comment