对于Java中的不同类型,泛型类的静态成员是不同的? [英] Are static members of a generic class different for different types in Java?

查看:150
本文介绍了对于Java中的不同类型,泛型类的静态成员是不同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Spence询问了这个先前的问题

那么,Java在这方面的工作如何?泛型类型在运行时会被抛弃,所以类的静态变量会发生什么变化,以不同的泛型类型实例化?

静态成员在Java中,不能从包含它们的类的泛型类型参数。

  public class Gen< T> {
public static T foo; //编译器错误
}


@Spence asked this Previous Question.

So, how's that work in Java? Generic types are discarded at runtime in Java, so what happens to static variables of classes instantiated with different generic types?

解决方案

Static members in Java can't have generic type arguments from the class that contains them.

public class Gen<T> {
  public static T foo; // compiler error
}

这篇关于对于Java中的不同类型,泛型类的静态成员是不同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆