static generic method returns object type
Hi,
I have a static generic method, something like:
publicstatic <E> SomeClass<E> myMethod(E arg){
returnnew SomeClass<E>(arg);
}
I would expect this method to return aSomeClass<E> (where by E I mean the type that was passed on in the argument) but instead it is returning aSomeClass<Object> .
Is there something I have forgotten, is this normal but can it be circumvented, is this a (known) bug or is there simply no way to get what I want?
Thanks,
qFox
[742 byte] By [
qFoxa] at [2007-10-2 20:14:08]

you need to cast the object before you return it, so:
public static <E> SomeClass<E> myMethod(E arg) {
return (E) new SomeClass<E>(arg);
}
How can you tell the difference between a SomeClass<E> and a SomeClass<Object>? There isn't a difference at run time, so presumably you're getting a compile error somewhere? Can you post more code so that the error can be seen in context and the exact text of the compiler error message.
http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#Why%20can%27t%20I%20use%20a%20type%20parameter%20in%20any%20static%20context%20of%20the%20parameterized%20class?
DrClap, does that article apply in this case? The OP is talking about a parameterized method, not a parameterized type. This is the same thing as, for example, Arrays.asList()
> you need to cast the object before you return it,
> so:
public static <E> SomeClass<E> myMethod(E arg)
{
return (E) new SomeClass<E>(arg);
}
I think that would result in a runtime error. The return type of the method is not E. Let us presume that the following call is made:
myMethod("test");
The object which is created by the method is a SomeClass<String>
but your code tries to cast it to a String
. That will result in an exception (not to mention that the cast is unchecked and will make your compiler fuss).
OP: The code you have posted looks fine to me. Can you post the compile-time error message you are getting as well as the call you are making to myMethod?
tvynra at 2007-7-13 22:56:18 >

> > you need to cast the object before you return it,
> > so:
>
> public static <E> SomeClass<E> myMethod(E arg)
> {
>return (E) new SomeClass<E>(arg);
> ode]
>
> I think that would result in a runtime error. The
> return type of the method is not E. Let us presume
> that the following call is made:
> [code]myMethod("test");
> The object which is created by the method is a
> SomeClass<String>
but your code tries to
> cast it to a String
. That will result
> in an exception (not to mention that the cast is
> unchecked and will make your compiler fuss).
>
> OP: The code you have posted looks fine to me.
> Can you post the compile-time error message you are
> getting as well as the call you are making to
> myMethod?
doh! yep, you're right
> I would expect this method to return a
> SomeClass<E> (where by E I mean the type that
> was passed on in the argument) but instead it is
> returning a SomeClass<Object> .
This method is parametrized by the type E. Let's suppose that SomeClass is ArrayList, ie.
public static <E> ArrayList<E> myMethod(E arg) {
ArrayList<E> l = new ArrayList<E>();
l.add(arg);
return l;
}
If you were calling this method, you will have something like
ArrayList<String> l = myMethod("bob");
This all compiles and works fine. What the compilation error you are seeing anyway?
Hm I'm sorry, I thought I marked that keep me updated checkbox when posting and didn't bother to check since I didn't get any email about replies. Anyways...
The compile errors occur when I try to use the returned value somewhere else. For instance when I do something like...
...
public static <E> ArrayList<E> myMethod(E arg) {
ArrayList<E> l = new ArrayList<E>();
l.add(arg);
return l;
}
public static void otherMethod(MyGeneric g) {
... (some MyGeneric specific code here)
}
...
public static void main(String[] a) {
MyGeneric x = new MyGeneric();
otherMethod(myMethod(x));
}
...
Then the compiler will tell me that otherMethod expects MyGeneric and not Object.
I expected the behaviour to be that E would be "casted" to whatever type the parameter is I use when calling myMethod(). So if I use it with an String, E would become a String (all E's would), if I passed on a Double, all E's become Double. So I don't really understand why Java returns an Object, unless my assumption was a little naieve...
qFoxa at 2007-7-13 22:56:19 >

Hrm... this compiles and works just fine. Maybe I made a typo of some kind the other day, I'm gonna try again. This same construction would tell me otherMethod expected ArrayList<Color>, not ArrayList<Object>.
public class Test {
public static enum Color { BLACK, BLUE, RED, WHITE, GREEN }
public static <E> java.util.ArrayList<E> myMethod(E arg) {
java.util.ArrayList<E> l = new java.util.ArrayList<E>();
l.add(arg);
return l;
}
public static void otherMethod(java.util.ArrayList<Color> g) {
System.out.println(g);
}
public static void main(String[] a) {
Color x = Color.BLACK;
otherMethod(myMethod(x));
}
}
qFoxa at 2007-7-13 22:56:19 >

Yep. Must have been something I missed, it works now. Thanks for the help anyways.
qFoxa at 2007-7-13 22:56:19 >
