I read Java Generics tutorials and a few threads in Stackoverflow that deal with Generics, and still could not figure out a specific case. There he is:
public class Box<T>
{
private T t;
public T getT ()
{
return t;
}
public void setT (T t)
{
this.t = t;
}
public static void main (String[] args)
{
Box<Integer> intBox = new Box<Integer>();
Box rawBox = intBox;
rawBox.setT("NBA");
System.out.println(rawBox.getT());
System.out.println(intBox.getT());
}
}
Here is the deal, the first seal that I understand, that is
System.out.println(rawBox.getT());
prints NBA because rawBox has a raw Box T type and it "gives" us objects.
What I am not getting is the second print:
System.out.println(intBox.getT());
NBA.
intBox ( Box of Integer), , getter T ( Integer), , String, , Integer ( , Box T), ClassCastException , , ?
, /1/ , , , , ClassCastException (String Integer), ,
:)