M
moleskyca1
The java compiler doesn't seem to allow the line (see below for full
code):
E item2 = new E();
with E a type parameter to a generic method.
Question 1: Do I have a typo/syntax error? Is this not compiling
because of a silly syntax error? Then the other questions are nill.
Question 2: How can work around this? We need to create a new instance
of the type represented by the type parameter in this generic method.
Question 3: Why the restriction? This seem restrictive. When working
with generics, I would expect to be able to instantiate a generic type;
that's a very elemental operation. I rekon it's becaise A Generic Class
is Shared by all its Invocations
Perhaps someone can elaborate.
import java.io.*;
import java.lang.*;
public class Test_q
{
public static void main(String []args)
{
Integer i = new Integer(7);
Test_q.f(i);
}
public static <E> void f(E item)
{
E item2 = new E();
}
}
code):
E item2 = new E();
with E a type parameter to a generic method.
Question 1: Do I have a typo/syntax error? Is this not compiling
because of a silly syntax error? Then the other questions are nill.
Question 2: How can work around this? We need to create a new instance
of the type represented by the type parameter in this generic method.
Question 3: Why the restriction? This seem restrictive. When working
with generics, I would expect to be able to instantiate a generic type;
that's a very elemental operation. I rekon it's becaise A Generic Class
is Shared by all its Invocations
Perhaps someone can elaborate.
import java.io.*;
import java.lang.*;
public class Test_q
{
public static void main(String []args)
{
Integer i = new Integer(7);
Test_q.f(i);
}
public static <E> void f(E item)
{
E item2 = new E();
}
}