Java – eclipse type erasure

I have a method defined like this:

public <T> T methodName(Class<T> clazz)

My unit test I mistakenly tested it as follows:

Class clazz = Tag.class;
Tag tag = methodName(clazz);

This should produce compile time errors (when I try to compile it with javac), but eclipse allows it to compile

I really don't understand why eclipse allows this code to be compiled. Someone can provide some information. Why is this compiled in eclipse?

The eclipse version is: Eclipse Java EE IDE for web developers Version: Luna service release 2 (4.4.2) build ID: 20150219-0600

The Java version used is: (Oracle) JDK 1.7 0_ fifty-one

Solution

Eclipse uses different compiler than the JDK – eclipse java compiler (ECJ) and javac Compilation mode, Java language specification interpretation and generated bytecode may be slightly different, of course, there are different errors You seem to have encountered one of the differences

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>