The Artima Developer Community
Sponsored Link

Java Answers Forum
Generic Class in Non Generic class

1 reply on 1 page. Most recent reply: Jun 21, 2006 11:58 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
cheran cheran

Posts: 1
Nickname: cheran
Registered: Jun, 2006

Generic Class in Non Generic class Posted: Jun 17, 2006 4:19 AM
Reply to this message Reply
Advertisement
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??


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Generic Class in Non Generic class Posted: Jun 21, 2006 11:58 PM
Reply to this message Reply
That doesn't make much sense, doas it?

You can't declare a instance variable without a class.
The compiler must know what you want to to.

Here is what you basically try to do.
        ArrayList ar = new ArrayList<String>(); //declared as ArrayList<Object>
        ar.add("Hello");
        ar.add(new Integer(5)); //this actually compiles, but during runtimme you could get some problems.
        String s = ar.get(0); // compile error. get(0) returns Object
        ar = new ArrayList<Integer>(); //still declared as ArrayList<Object>
        ar.add(new Integer(5)); //using autoboxing here
        Integer i = ar.get(0); //compile error


Tell me, what you try to achieve, then I might be able to help you find a solution for it.

Flat View: This topic has 1 reply on 1 page
Topic: chart in java Previous Topic   Next Topic Topic: retaining the format of the properties file while saving it from Java!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use