In one of the arguments about differences between lambdas and anonymous classes, in this post:
Java8 Lambdas vs Anonymous classes
I read a claim that "Lambdas can have state" just like anonymous class instances.
As far as I know, you cannot add user defined state that belongs exclusively to the lambda , since there is no way to define instance members on an implementation of a java lambda function.
For example:
Runnable r= () -> { int x = 5; }; // defines a local - no way to define instance
Runnable r2 = new Runnable() {
int x; // defines state via instance member
@Override
public void run() {
// TODO Auto-generated method stub
}
};
Just to clarify, I am not trying to introduce state to a lambda, as I think that goes against the intent. I am just trying to verify or disprove a claim of a technical nature that was made by a reputable source on the above stack overflow question.