For most VMs, if the code executed encounters the class as a variable type
it will ask the ClassLoader to load it. If you want to prevent a class
from loading you need to prevent any references to the class from occuring
except in the exact context when you want it to be loaded.
> Or, should I provide stubbed-out class files for the only two classes
> that are directly referenced, DTD and Validator?
>
> Or, is this worth worrying about? Or is there a standard way to
> achieve this effect? Wisdom welcome. -Tim
The simplest approach would be to define an interface or abstract class
for the DTD and validator that has the minimum your main class needs.
This will always be loaded with the main class. Then you can
implement/subclass off this interface to put the full validation
functionality in. Finally, have exactly one method in the main class that
constructs an object of the full implementation (assigning it to a
variable of the general type) and only call this method when you need to.
The full implementation classes should only be loaded when this
particular method is called, so (for your example) only when validation
is turned on.
You can verify what is happening with loading by turning on 'java' verbose
mode to see if everything is working ok. Some VMs behave differently
(delayed loading is not part of the Java spec, just common), but I think
most VMs behave this way.
Hope that helps.
--Mark
mark.fussell@chimu.com