[英]Using generic return value in static method Java
here is my code 这是我的代码
class MyClass<E> {
public static List<E> myUtilMethod(){
}
}
Once I compile this, I get this error 编译之后,我会收到此错误
non-static type variable E cannot be referenced from a static context
非静态类型变量E不能从静态上下文中引用
How I can return a generic type from this method? 如何从此方法返回通用类型?
You need to add the type parameter to your myUtilMethod
method... 您需要将类型参数添加到
myUtilMethod
方法中...
public static <T> List<T> myUtilMethod() {
^
|------- here!!
// TODDY
return someList;
}
edit: 编辑:
as makoto spotted, is in that case not the same as the defined in MyClass
, though. 就像makoto发现的那样,在这种情况下与
MyClass
的定义不同。 you should rename that generic type to something else... (T in the code I posted!) 您应该将该通用类型重命名为其他类型...(我发布的代码中的T!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.