6.6 Do External References Check Out?

An external reference is an attempt to use a field or method of a class that isn't the class you're currently checking. Even if the class was defined in the same Java source file, it's still an external reference because it comes from a different class file.

Checking external references is a matter of loading all the classes referenced in the constant pool. These are loaded using the same class loader that used this class. (See chapter 8 for more on class loaders). When loading a class, you need to ask,

  • Does the superclass list avoid looping?

To put it another way,

  • Is there any way for a class to be a superclass of itself? If so, the class is invalid.

For example, the following is invalid:

 .class Concept .super Idea .end class .class Idea .super Thought .end class .class Thought .super Concept                 ; Whoops! Circularity. .end class 

Then, for each field or method accessed by the class you're checking, ask the following questions:

  • Does the class have a field or method with the appropriate name and type?

  • Does this class have permission to use it?

This way of checking external references gives JVM programmers tremendous flexibility to change classes without having to recompile all the classes that use them. You only have to check for the fields and methods that are actually used. You can add or delete fields and methods, without altering the correctness of classes that don't use them.

In addition, because they're checked by name and type, you can move methods around in the source file without affecting other classes that use this class. This helps solve the "fragile base class" problem familiar to C++ programmers, who know that even a seemingly negligible change such as changing the order of methods can require recompilation of every file in the project. It is a cause of many subtle and mysterious bugs in C++ projects.

Many JVM implementations, including Sun's Java Development Kit, don't actually test external references until they are needed. This can save a lot of time. You can start using the class immediately, without having to wait for all the related classes (and all the classes related to those classes, and so on) to load. It's quite possible that many of the references will never need to be checked because the field or method is never used.



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net