3 ms·
What's correct here is that the target of a method reference is evaluated when the reference is evaluated. What's incorrect is that references to static (or ot
by twic 10y ago
What's correct here is that the target of a method reference is evaluated when the reference is evaluated.
What's incorrect is that references to static (or other) variables are somehow treated differently. The author has misinterpreted their own code - see jwolfe's and my comments on the post.
- Clee681 10y agoHey @twic, Does the fact that Java is "pass by value" explain why the reassignment of "obj" does not change the output of re-invoking the method ref?
- twic 10y agoKind of. When i see a method reference like this: someExpression()::methodName I mentally rewrite it into: getMethodReference(someExpression(), "methodName") Where there is some fictional getMethodReference function that takes an object and a method name, and creates a reference. It works like this even when someExpression() is just a reference to a variable. In the rewritten form, someExpression() gets fully evaluated before getMethodReference is called, producing a value, and it's that value which is used to create the reference. As you suggested, it works like that because Java is pass-by-value. If Java was pass-by-reference, then someExpression() would produce a reference to a variable instead of a value, and the reference could somehow be based on that variable. But it doesn't, so it's not.
- Clee681 10y agoAwesome, thanks for the clarification!