JVM Knowledge

There are six cases to consider, depending on the form of the method invocation:

  • If the form is MethodName - that is, just an Identifier - then:

    • If the invocation mode is static, then there is no target reference.

    • Otherwise, let T be the enclosing type declaration of which the method is a member, and let n be an integer such that T is the n'th lexically enclosing type declaration of the class whose declaration immediately contains the method invocation. The target reference is the n'th lexically enclosing instance of this.

      It is a compile-time error if the n'th lexically enclosing instance of this does not exist.

  • If the form is TypeName . [TypeArguments] Identifier, then there is no target reference.

  • If form is ExpressionName . [TypeArguments] Identifier, then:

    • If the invocation mode is static, then there is no target reference. The ExpressionName is evaluated, but the result is then discarded.
    • Otherwise, the target reference is the value denoted by ExpressionName.
  • If the form is Primary . [TypeArguments] Identifier involved, then:

    • If the invocation mode is static, then there is no target reference. The Primary expression is evaluated, but the result is then discarded.
    • Otherwise, the Primary expression is evaluated and the result is used as the target reference.

    In either case, if the evaluation of the Primary expression completes abruptly, then no part of any argument expression appears to have been evaluated, and the method invocation completes abruptly for the same reason.

  • If the form is super . [TypeArguments] Identifier, then the target reference is the value of this.

  • If the form is TypeName . super . [TypeArguments] Identifier, then if TypeName denotes a class, the target reference is the value of TypeName.this; otherwise, the target reference is the value of this.

Example 15.12.4.1-1. Target References and static Methods

When a target reference is computed and then discarded because the invocation mode is static, the reference is not examined to see whether it is null:

class Test1 {
    static void mountain() {
        System.out.println("Monadnock");
    }
    static Test1 favorite(){
        System.out.print("Mount ");
        return null;
    }
    public static void main(String[] args) {
        favorite().mountain();
    }
}

which prints:

Mount Monadnock

Here favorite() returns null, yet no NullPointerException is thrown.

Example 15.12.4.1-2. Evaluation Order During Method Invocation

As part of an instance method invocation (§15.12), there is an expression that denotes the object to be invoked. This expression appears to be fully evaluated before any part of any argument expression to the method invocation is evaluated.

So, for example, in:

class Test2 {
    public static void main(String[] args) {
        String s = "one";
        if (s.startsWith(s = "two"))
            System.out.println("oops");
    }
}

the occurrence of s before ".startsWith" is evaluated first, before the argument expression s = "two". Therefore, a reference to the string "one" is remembered as the target reference before the local variable s is changed to refer to the string "two". As a result, the startsWith method is invoked for target object "one" with argument "two", so the result of the invocation is false, as the string "one" does not start with "two". It follows that the test program does not print "oops".