The following code compiles both test methods using javac in JDK7 but JDK8 will only compile willCompile
method.
The error for willNotcompile
is:
"The method method
(Class<T>
) in the type Klasa
is not applicable for the arguments (Class
)."
@Test
public void willCompile() throws InstantiationException, IllegalAccessException {
Class klass = getObject(Class.class);
method(klass);
}
@Test
public void willNotCompile() throws InstantiationException, IllegalAccessException {
method(getObject(Class.class));
}
<T> ResponseEntity<T> method (Class<T> klasa) {
return new ResponseEntity<T>(HttpStatus.OK);
}
public static <T> T getObject(Class<T> clazz) throws IllegalAccessException, InstantiationException {
return clazz.newInstance();
}
As Cootri has stated - you can install any number of Java versions on windows machines.
The backwards compatibility means that you can run Java 7 program on Java 8 runtime, not the other way around. There are several reasons for that: Bytecode is versioned and JVM checks if it supports the version it finds in . class files.
http://www.oracle.com/technetwork/java/javase/jdk8-naming-2157130.html Java SE Development Kit 8, also known as JDK 8, has the version number 1.8. In short – 8 is product version number and 1.8 is the developer version number (or internal version number). The product is the same, JDK 8, anyways.
The above compiles because it is using rawTypes.
The bottom one doesn't because your method only accepts a Class<T>
, but you gave it a Class
. Using reflection, you cannot specify the generic types of a class, so getObject
will return a raw Class object.
The only fix for the problem is casting the return result.
method((Class<?>)getObject(Class.class));
But while this solution solves the runtime problem you still get problems with the fact that you cannot create new instances of Class
.
JDK 7 was less strict in this comparison and casted the return result Class
into a Class<?>
behind the scenes so the code was allowed to compile.
According to Holger JDK 7 turns off generics types for the whole lines, and uses raw types for the return result, meaning that method gets a Class
and returns a ResponseEntity
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With