DRAFT
CallRequiresVariable
Misconception:
To invoke a method, one requires a variable that points to an object (e.g., only given a variable like o
can one call a method: o.m()
).
Incorrect
One needs a variable to invoke a method
Correct
CorrectionHere is what's right.
Here is what's right.
One can invoke a method on an object without the reference to that object being stored in a variable. All that is needed is that the expression in front of the ”.” of the call evaluates to an object reference.
For example, one can do method chaining on a normal method:
get().m(); // assuming get() returns an object
Or one can do method chaining on a constructor:
new C().m();
Or one can call a method on an String
literal:
"Hi".toString();
Or one can call a method on an object one gets out of an array:
a[0].m();