It can be reused, it may not be so.
From JLS 15.27.4 :
Either a new instance of the class with the following properties: selected and initialized, or an existing instance of the class with the properties below.
You cannot depend on this one or the other. The compiler and / or runtime can choose the one that will give the best result. (This is one of the advantages of lambdas over anonymous classes - since every time you use new , even in an anonymous class, it is guaranteed to be a new object, it cannot optimize it by reusing it, although in 99% of cases you don’t whether they are the same object or not.)
In the case where the lambda captures variables from the surrounding area, it is usually impossible to reuse the object, because the value of the captured variables is a state stored in the lambda object, and every time the lambda is calculated (even if it is the same lambda in source code), it can capture different values of the captured variables. Only if the compiler can somehow guarantee that two specific lambda evaluations should fix the same value of the variables, can the compiler reuse the object.
In the case when the lambda does not fix any variables, all instances of this lambda are identical to the behavior. Thus, in this case, one object can be reused for all evaluations of this lambda. I believe that the current Java implementation in this case allocates only one instance for the duration of the program. But this is just implementation dependent optimization.
newacct
source share