Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class Cast exception

0 views
Skip to first unread message

jacov

unread,
Aug 11, 2004, 3:39:02 AM8/11/04
to
HI folks, i have a silly problem. Does anyone Know why casting ArrayList
to List ( e viceversa)is a problem:
e.g. public List aMethod() {.. return ArrayList} //no problem
but ArrayList list = (ArrayList) aMethod --> ClassCastException

Thanks

jacov

Christophe Vanfleteren

unread,
Aug 11, 2004, 3:45:45 AM8/11/04
to
jacov wrote:

It isn't a problem. You are able to cast a List to ArrayList, if that List
*is* an ArrayList (or a subclass).

I'm pretty sure the the List aMethod returns isn't an ArrayList if you get a
ClassCastException.

--
Kind regards,
Christophe Vanfleteren

jacov

unread,
Aug 11, 2004, 3:51:06 AM8/11/04
to
THis is a very long standing problem in my programs, so i'm sure of
this. But in don't understand why...

Christophe Vanfleteren

unread,
Aug 11, 2004, 4:04:25 AM8/11/04
to
jacov wrote:

Please don't toppost.

> THis is a very long standing problem in my programs, so i'm sure of
> this. But in don't understand why...
>

I'm sure it isn't. I'll repeat, if your method actually returns an
ArrayList, the cast *will* go fine. If it isn't, you'll get the
ClassCastException. It is that simple.

If you keep getting the exception, try printing the name of the class of the
instance that is returned from the method.

Also check this small test:

import java.util.*;

public class Test {

public List getList() {
return new ArrayList();
}

public List getLinkedList() {
return new LinkedList();
}

public static void main(String[] args) {
Test t = new Test();
ArrayList a = (ArrayList)t.getList();//will work
ArrayList c = (ArrayList)t.getLinkedList();//will not work

Chris Smith

unread,
Aug 11, 2004, 9:58:48 AM8/11/04
to
Christophe Vanfleteren wrote:
> I'm sure it isn't. I'll repeat, if your method actually returns an
> ArrayList, the cast *will* go fine. If it isn't, you'll get the
> ClassCastException. It is that simple.

For the most part, it's that simple. However, here's some things to
watch out for:

1. Two classes can have the same name, but be different because they are
in different packages, or one is an inner class of another, or some such
thing. If you've written your own class called ArrayList, then there
may be some confusion as to which ArrayList you're working with.
There's also a (non-public) API class called java.util.Arrays$ArrayList
-- IIRC -- which is quite different from the standard
java.util.ArrayList class. So the class being called ArrayList isn't
good enough; it has to actually be the same class.

2. If there are multiple classloaders involved (whether you used them
explicitly or you're working in an environment such as a servlet
container or J2EE application server that uses them implicitly) then
there are a few more ways this can go wrong even for classes with the
same fully qualified name.

3. It's possible to forget that doing something like
Collections.synchronizedList or Collections.unmodifiableList will
actually return a List of a different implementation class than the
original list that was passed into the method. You could catch this one
with a simple print of getClass().getName(), as Christophe suggested.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Stefan Schulz

unread,
Aug 13, 2004, 3:49:32 PM8/13/04
to
Chris Smith wrote:

> 1. Two classes can have the same name, but be different because they are
> in different packages, or one is an inner class of another, or some such
> thing. If you've written your own class called ArrayList, then there
> may be some confusion as to which ArrayList you're working with.
> There's also a (non-public) API class called java.util.Arrays$ArrayList
> -- IIRC -- which is quite different from the standard
> java.util.ArrayList class. So the class being called ArrayList isn't
> good enough; it has to actually be the same class.

Don't forget java.awt.List as well. This one has caused me grief when i
trusted a little too much on Eclipse quickfixes. If you say something like

import java.awt.List;

public class Foo {
void bar () {
List l = (List) new ArrayList();
}
}

you will rightfully get a CCE

0 new messages