java - When inserting objects into a type-safe heterogeneous container, why do we need the class reference? -


i'm checking out heterogeneous container pattern bloch's effective java , i'm trying determine why class reference needed when inserting objects heterogeneous container. can't use instance.getclass() reference? isn't jpa's entity manager example of this?

interface blochsheterogeneouscontainer {      <t> void put(class<t> clazz, t instance);      <t> t get(class<t> clazz); }  interface alternativeheterogeneouscontainer {      // class<t> not needed because can use instance.getclass()     <t> void put(t instance);      <t> t get(class<t> clazz); } 

no can't that, won't give class of reference type in case of inheritance, rather class of actual object type.

consider example:

number num = new integer(4); system.out.println(num.getclass()); 

this print:

class java.lang.integer 

and not java.lang.number.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -