Cannot New a Type Parameter?

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();
}

}
 
O

Oliver Wong

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.

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
 
C

Christopher Benson-Manica

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top