Hacker News new | ask | show | jobs
by Clee681 3529 days ago
Hey @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?

1 comments

Kind 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.

Awesome, thanks for the clarification!