This question already has an answer here:
- Can a local variable with an inferred type be reassigned to a different type? 2 answers
Java 10 allows to do an anonymous class
with a var
like:
var a1 = new Object(){};
var a2 = new Object(){};
But this assignment will throw an error:
a1 = a2;
jshell> a1 = a2; | Error: | incompatible types: $1 cannot be converted to $1 | a1 = a2; | ^^
Based on the error log, why can't Java 10 assign two inferred var
s as an anonymous class
to each other, but it can do the same for other types like Long
, String
, etc.?