Every Java Class
has a reference to the class loader that loaded it. Classes of the same name loaded by two different class loaders are considered to be completely different. This will cause instanceof
checks to fail, even if the class is identical in both loaders. As a result, you need to be very careful when using the disposable Class Loader feature. It is very easy to find yourself in a situation where you have two components that appear to be identical, but are in fact inconsistent.
For instance, suppose you:
load a class named
Arbitrary
,instantiate it as object
arbitrary1
,switch class loaders,
reload class
Arbitrary
, andinstantiate it as
arbitrary2
.
If arbitrary2
resolves arbitrary1
as an Object and casts it to the type Arbitrary
, a ClassCastException
will be thrown. The disposable Class Loader feature, therefore, is really most helpful when you have a set of classes to be reloaded whose instances are easy to completely expunge from the server prior to creating a new Class Loader.