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();
}
}
Oliver Wong - 28 Jul 2006 21:21 GMT
> The java compiler doesn't seem to allow the line (see below for full
> code):
[quoted text clipped - 4 lines]
> 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.
The other questions are not 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.
You could pass in a class object, and call newInstace() on it, or query
for what constructors are available and somehow figure out which one to
invoke, and what the appropriate parameters would be.
> 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
Not sure "why" exactly, but one problem you'd run into is what if the
type passed in doesn't implement the 0-parameter constructor, or what if
that constructor isn't visible?
- Oliver
Christopher Benson-Manica - 28 Jul 2006 21:26 GMT
> 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.
Nope, it just doesn't work that way. Sun has a tutorial on generics
that you may find illuminating:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
> 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.
The above document describes the use of class literals for working
around this limitation of generics.
> 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
From the document: [T]ype variables don't exist at runtime. This is
simply a factor of how generics are implemented in Java; if you are,
as I suspect, coming from a C++ background, you will find many
differences between Java generics and C++ templates.

Signature
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.