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 / July 2006

Tip: Looking for answers? Try searching our database.

Generic class in a non generic class

Thread view: 
nramnath@gmail.com - 17 Jun 2006 12:21 GMT
Hi,
I want to store a Generic class in a non generic class. Is it possible
??

public class A<T>
{
T t ;
public T getValue()
{
return t;
}
}

public class B
{
A a; // How to declare generic A here
public <T> A<T> getA()
{
return a;
}
}

I know this will not work. Is there a way I can get it working with
wildcards.
I am not 100% clear with generic - Is it possible at all??
Chris Smith - 17 Jun 2006 17:00 GMT
> public class A<T>
> {
[quoted text clipped - 17 lines]
> wildcards.
> I am not 100% clear with generic - Is it possible at all??

If I understand you correctly, then no it's not possible, at least
without warnings.  That's because it's not type-safe.  You are basically
saying that you want B to not keep track of the type parameter, and then
be able to return the object as if it had any type parameter and have
the compiler be okay with it.  That's exactly the kind of problem that
generics were meant to solve.  You certainly can do this:

public class B
{
   A<?> a;
   public A<?> getA() { return a; }
}

That's okay because you aren't pretending to know anything about a.  
And, of course, you can do this:

public class B<T>
{
   A<T> a;
   public A<T> getA() { return a; }
}

That's okay because you really do know something about a.  The only
thing that's truly impossible is to throw away knowledge of a's type,
and then go on as if you still know it by trying to return a as a
specific type.

What you're attempting is actually somewhat close to capture conversion.  
If you had an a for which you don't know the type argument, you can
introduce an arbitrary type name to represent it for the purposes of a
calculation.  However, you can't then return something typed with that
arbitrary name, since the name is meaningless outside of the method
which resulted in use of capture conversion.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

nramnath@gmail.com - 04 Jul 2006 08:24 GMT
Hai,
Thanks for the reply. I too know about that, but just wanted to
confirm.
Thanks
Ramnath
> > public class A<T>
> > {
[quoted text clipped - 55 lines]
> Chris Smith - Lead Software Developer / Technical Trainer
> MindIQ Corporation


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.