I am using jdk 1.6
My main method :
exmp3<String , Integer> exmp3 = new exmp3( "ravi" , new ArrayList<String>());
exmp3.put(5);
exmp3.put(15);
exmp3.put(20);
ArrayList<Integer> a = exmp3.getObject();
System.out.println(a.size());
class exmp3 :
public class exmp3<ABC , XYZ>
{
private ABC abc ;
private ArrayList<XYZ> xyz;
public exmp3(ABC abc , ArrayList<XYZ> xyz)
{
this.xyz = xyz;
}
public void put(int i)
{
ArrayList<Integer> a = (ArrayList<Integer>) this.xyz;
a.add(i);
}
public ArrayList<XYZ> getObject()
{
return xyz;
}
}
My query is while creating instance of exmp3 in main method why String is accepted as parametric type of ArrayList where as i have mentioned exmp3<String , Integer> exmp3 in main method.
Because you are using this :
which is almost the same as
Arguments in methods or constructors never defines the types when instancing. the compiler only sees (if you don’t think about erasure) :
while your instance is declared to use this definition :