[英]Why not raw type, static variable in generic class?
为什么不是普通类中的原始类型,静态变量?
例,
public class MyType<E> {
class Inner { }
static class Nested { }
public static void main(String[] args) {
MyType mt; // warning: MyType is a raw type
MyType.Inner inn; // warning: MyType.Inner is a raw type
MyType.Nested nest; // no warning: not parameterized type
MyType<Object> mt1; // no warning: type parameter given
MyType<?> mt2; // no warning: type parameter given (wildcard OK!)
}
}
MyType
是泛型类, Nested
是static
类。
名为MyType.Inner
,不是警告泛型类型。
我想知道为什么static
变量不是警告原始类型?
“在以下任何地方引用泛型类C的类型参数是编译时错误:
8.5.1引用静态嵌套类。
(JLS§8.1.2]( http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.2
因此,静态嵌套类不会也不能从外部类中获取与之关联的任何通用信息,因此不会对其缺失发出警告。
因为静态嵌套类Nested
不与MyType
任何特定实例相关联。 泛型类型(仅在编译时)应用于您创建MyType
的实例,并且由于Inner
与此类实例相关联,因此它可以引用泛型参数E
,但Nested
没有此类连接。
由于类型擦除,Java泛型只能应用于对类实例的引用。
因此,嵌套静态类中根本不会继承泛型参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.