10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPRESSIONS Compile-Time Step 3: Is the Chosen Method Appropriate? 15.12.3<br />

because the old binary file for Test still has the descriptor “one parameter, whose<br />

type is Point; void” associated with the method call cp.adopt(cp2). If the<br />

source code for Test is recompiled, the compiler will then discover that there are<br />

now two applicable adopt methods, and that the signature for the more specific<br />

one is “one parameter, whose type is ColoredPoint; void”; running the program<br />

will then produce the desired output:<br />

cp: (3,3,green)<br />

With forethought about such problems, the maintainer of the points package<br />

could fix the ColoredPoint class to work with both newly compiled and old<br />

code, by adding defensive code to the old adopt method for the sake of old code<br />

that still invokes it on ColoredPoint arguments:<br />

public void adopt(Point p) {<br />

if (p instanceof ColoredPoint)<br />

color = ((ColoredPoint)p).color;<br />

x = p.x; y = p.y;<br />

}<br />

Ideally, source code should be recompiled whenever code that it depends on is<br />

changed. However, in an environment where different classes are maintained by<br />

different organizations, this is not always feasible. Defensive programming with<br />

careful attention to the problems of class evolution can make upgraded code much<br />

more robust. See §13 for a detailed discussion of binary compatibility and type<br />

evolution.<br />

15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?<br />

If there is a most specific method declaration for a method invocation, it is called<br />

the compile-time declaration for the method invocation. Three further checks<br />

must be made on the compile-time declaration:<br />

• If the method invocation has, before the left parenthesis, a MethodName of<br />

the form Identifier, and the method is an instance method, then:<br />

DRAFT<br />

◆ If the invocation appears within a static context (§8.1.3), then a compiletime<br />

error occurs. (<strong>The</strong> reason is that a method invocation of this form cannot<br />

be used to invoke an instance method in places where this (§15.8.3) is<br />

not defined.)<br />

◆ Otherwise, let C be the innermost enclosing class of which the method is a<br />

member. If the invocation is not directly enclosed by C or an inner class of<br />

C, then a compile-time error occurs<br />

• If the method invocation has, before the left parenthesis, a MethodName of<br />

the form TypeName.Identifier, or if the method invocation , before the left<br />

471

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!