Allocating a Parameterized Object

B

Bruce Feist

I have a generic class ClassX defined with a parameterized type T of
subclass ClassY. Within a ClassX method, I want to allocate a new
object of type T. When I try using:

ClassY foo = new T();

I get a message telling me that:
Maze.java:25: unexpected type
found : type parameter T
required: class

How can I accomplish this?

Thanks,
Bruce Feist
 
S

Stefan Ram

Bruce Feist said:
I have a generic class ClassX defined with a parameterized type
T of subclass ClassY. Within a ClassX method, I want to
allocate a new object of type T.

This sounds like an FAQ.

The type T is not known at run-time
Therefore, it can not be used at run-time.

An approach to create a new object of type T might be
(untested):

public class Example< T >
{ final Factory< T >factory;
public Example( final Factory< T >factory ){ this.factor = factory; }
public void example(){ final T entry = this.factory.newInstance(); }}
 
B

Bruce Feist

Stefan said:
The type T is not known at run-time
Therefore, it can not be used at run-time.

This makes sense; thanks. Your use of the Factory class confused me a
bit; were you suggesting that I create an extra auxiliary class in order
to create the objects?

As an alternative to what I think you were suggesting, I'm going to try
passing the parameterizing class as a parameter to the constructor,
which is logically redundant with using the parameter in the first
place. This will be stored in the object, and will let me use the
newInstance method.

The actual statement that I'm using to invoke the constructor is now:

Maze m = new Maze<TriangleCell> (nRows, nColumns,
mazeFrame.getContentPane(), TriangleCell.class);

It compiles; I don't yet have enough program in place to see if it works.

Thanks again for your help.
Bruce
 
S

Stefan Ram

Bruce Feist said:
passing the parameterizing class as a parameter to the constructor,
which is logically redundant with using the parameter in the first
place. This will be stored in the object, and will let me use the
newInstance method.

A class might be abstract or might lack a proper (default)
constructor, so the availability of a newInstance-method can
not be checked statically.
 
B

Bruce Feist

Stefan said:
A class might be abstract or might lack a proper (default)
constructor, so the availability of a newInstance-method can
not be checked statically.

And so I might get a run-time error. For this application, I can deal
with that as a trade-off with the complexity of using an auxiliary
factory class. If I were writing a package for commercial distribution
I'd rethink that, but I'm likely to be the only one ever using this.

Or maybe I'm just being lazy...

Bruce
 

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
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top