Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Allocating a Parameterized Object

Thread view: 
Bruce Feist - 23 Oct 2006 02:24 GMT
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
Stefan Ram - 23 Oct 2006 02:31 GMT
>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(); }}
Bruce Feist - 23 Oct 2006 03:29 GMT
>> I want to allocate a new object of type T.
>
>   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
Stefan Ram - 23 Oct 2006 03:58 GMT
>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.
Bruce Feist - 23 Oct 2006 04:07 GMT
>> passing the parameterizing class as a parameter to the constructor,
>
>   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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.