What can be the reason for this to not work?
ThreadLocal<Integer> intLocal2 = new ThreadLocal<>() {
@Override
protected int initialValue() { // if I use int as return type it doesn't work
return 0;
}
};
but if I use Integer as return type of initialValue() it will work.
I tried this on java 11
>Solution :
Autoboxing is impossible for the return types, see the docs:
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
Converting a primitive value (an int, for example) into an object of
the corresponding wrapper class (Integer) is called autoboxing. The
Java compiler applies autoboxing when a primitive value is:
- Passed as a parameter to a method that expects an object of the
corresponding wrapper class.- Assigned to a variable of the corresponding wrapper class.