Showing posts with label JBM. Show all posts
Showing posts with label JBM. Show all posts

Tuesday, December 11, 2007

ClassCastException Solution

The obvious cause for ClassCastException is that you try to cast from one class to another class but the other class is not a parent of the class you try to cast, but there are other cases in which ClassCastException can happen:

AClass aObject = (AClass) bObject;

- bObject doesn't inherit from AClass or it doesn't AClass instance.

- bObject inherit from AClass or it is AClass instance, however bObject was loaded by a different class loader, In this case the JVM consider bObject as a different class, the solution is to load AClass only in one of the class loaders, the same class shouldn't be loaded by two different class loaders.

For example:
Object a1 = new A(); // Load by classLoader x
Object a2 = new A(); // Load by classLoader y

a1 = (A) a2; // Would throw ClassCastException

Links

 
RSS Feeds Submission Directory