Java Generics create an instance of type parameter

Question

What is the output of the following code?

class Gen<T> {  
  T ob;  /* w ww. ja va2 s  .  c  o m*/
  Gen() {  
    ob = new T(); 
    System.out.println(ob);
  }  
} 

public class Main{
  public static void main() {
    Gen<String> g = new Gen<>();
    Gen<Integer> g2 = new Gen<>();
  }
}


Compile time error
Type Parameters Can't Be Instantiated

Note

It is not possible to create an instance of a type parameter.

The compiler does not know what type of object to create.

T is simply a placeholder.




PreviousNext

Related