1

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?

user207421
  • 305,947
  • 44
  • 307
  • 483
Yuki Hashimoto
  • 1,013
  • 7
  • 19
  • I think your question title is misleading: as it stands, I'm pretty sure it's a dupe of another question, but your body actually asks specifically about serialization, maybe update the title to reflect that. Also, I don't think Java 11 changed anything about that behaviour, so any answer for Java 8 might still be applicable. – Joachim Sauer Dec 04 '19 at 09:45
  • 1
    What happens if you try a lambda expression that refers to a field in the surrounding object? – RealSkeptic Dec 04 '19 at 09:48
  • @RealSkeptic It raises `NotSerializableException`, because `field` is a syntax sugar of `this.field`, thus `this` is captured. – Yuki Hashimoto Dec 04 '19 at 09:52
  • `NotSerializableException` names the class that wasn't serializable. What was it? NB Every time you have written 'serialized' here you should have written 'serializable'. – user207421 Dec 04 '19 at 09:54
  • So from that you can deduce that in some cases, lambda expressions are serializable, and in some cases, they are not. Obviously, they are not just syntactic sugar for anonymous classes, because in some cases - as you have shown above - they behave differently. – RealSkeptic Dec 04 '19 at 09:54
  • 1
    @RealSkeptic Yes, it may be. However, I want to know whether this behavior is an officially define or just dependent to my runtime environment. – Yuki Hashimoto Dec 04 '19 at 09:56
  • If the lambda does not actually use `this`, then it does not have to be captured. – Thilo Dec 04 '19 at 09:58
  • 1
    Also see https://stackoverflow.com/a/27524543/14955 – Thilo Dec 04 '19 at 10:03
  • @ThiloThank you very much! runtime-environment dependent...OMG... – Yuki Hashimoto Dec 04 '19 at 10:16
  • 1
    @YukiHashimoto My reading of these answers is that "runtime-environment dependent" only refers to sharing of singleton instances. The part about not capturing `this` unless needed is probably part of the spec: https://stackoverflow.com/a/28447015/14955 – Thilo Dec 04 '19 at 12:08
  • @Thilo Thanks again! I'm relieved to have confirmed that non-captured `this` is a spec of lambda! – Yuki Hashimoto Dec 05 '19 at 06:05

0 Answers0