Search code examples
javadefinitions

In object-oriented programming, is the object part of the method call?


For example, imagine I have an object defined like so:

public class Example {
    public void doSomething() {
        // does something
    }
}

If I wanted to call doSomething, I'd need an instance of Example:

Example foo = new Example();
foo.doSomething();  // doSomething is executed

My question is, what part of the line foo.doSomething(); is officially considered the method call?

Is it just the doSomething() part or is it the entire statement including the object (foo.doSomething())?


Solution

  • In Java, the whole target.method() is considered part of the method invocation:

    http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448

    This means that if you have code like this:

    foo.bar().baz()
    

    ...then the whole expression is the method invocation of baz(), and the target of that method invocation is itself another method invocation.