Why calling method with generic return on a generic class is considered unsafe by javac?

后端 未结 4 1688
南笙
南笙 2020-12-06 16:42

Consider the following code:

public class Main {
    public static class NormalClass {
        public Class method() {
            return Inte         


        
4条回答
  •  难免孤独
    2020-12-06 17:03

    In order to be compatible with Java 1.4 compiler assumes that if you drop the generic arguments on instance type declaration, then you work with the special version of the class where no generics exist at all. And it issues a warning if you mix a Java 1.4 non-generic code with Java 1.5+ generic code. That's easier than trying to figure out whether generic return type of your method is actually independent from parameters. You can always @SuppressWarning if you don't like it.

提交回复
热议问题