0

Learning Java here, and specifically about type parameter. Can you explain to me why this code compiles?

public static void main(String args[]){
    bar a = new bar(    1);
    System.out.println(a.name);
    a.speak(a);
    System.out.println(a.name);
}

public static class bar<T>{
    public T name;

    public bar(T name) {
        this.name = name;
    }

    public void speak(bar a){
        a.name = "I AM A STRING";
    }
}

After all - T is set to be an int when a is initallized, how can it change it's type to a String during the run?

Golden
  • 407
  • 2
  • 12

0 Answers0