I'm planning to serialize a lambda expression in Java 11. However, I'm not sure about the exact behavior of lambda expression in Java 11.
For simplicity, here I define SerializableFunction
as an intersection type of Function
and Serializable
.
I know anonymousInnerClass
object in the code below should not be serializable due to implicit synthetic this
binding.
SerializableFunction<Object, Object> anonymousInnerClass = new SerializableFunction<Object, Object>() {
@Override
public Object apply(Object o) {
return null;
}
};
If the lambda expression is just a syntax-sugar for an anonymous inner class, the lambdaExpression
object should not be serializable either.
Function<Object, Object> lambdaExpression = (o) -> null;
However, in my environment, the former code raises NotSerializableException
, but the latter one does not.
Is there any official description about this behavior?