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
Tuesday, December 11, 2007
ClassCastException Solution
Posted by
blogger
at
10:54 PM
Labels: ClassCastException, ClassLoader, JBM, Solution
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment