wood burning stoves*
The moose likes Java in General and the fly likes ClassCastException in Tag Library Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassCastException in Tag Library" Watch "ClassCastException in Tag Library" New topic
Author

ClassCastException in Tag Library

Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
I have an object that is implemented, fundamentally, as an ArrayList:



The method as shown above, getItems() is what I'm trying to do.

In another class (this is a Tag Library, if that makes a difference), I have the following attempt to access this list as an array:



All the following combinations compile fine, but throw a ClassCast Exception when tested:

Combo 1:



Combo 2:



And, it always references the line, regardless of which class it is in, where I attempt to do the cast from Object to menuitem[].

However, interestingly enough (in my opinion) this does work:



Obviously, the difference is that no cast is required in the last (working) example.

Can someone tell me what's going on here? Why doesn't the cast work? And is there any way to do this with ArrayList's built-in toArray function (which I believe requires a cast in any case)?

TIA


-------<br />Thanks & regards,<br />anw
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Since this is more of a fundamental Java question than a taglib specific one, I'm going to move this to Java In General (Internmediate).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Obviously, the difference is that no cast is required in the last (working) example.

Can someone tell me what's going on here? Why doesn't the cast work? And is there any way to do this with ArrayList's built-in toArray function (which I believe requires a cast in any case)?


There are two overloaded versions of the Collection.toArray() method. The default one (no parameters) return everything in an Object array. An object array can hold any type of objects. However, just because all the items in the array are menu item objects, doesn't make the array a menu item array -- it is still an object array.

The other overloaded method is what you need. It takes an array that it will either use directly, or create another by type, and return an object array that IS-A type of array that you want. Basically....



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
Thanks, Henry! I did what you said, and it worked as advertised. I still don't understand, though, why the other way couldn't be cast. Let me ask this:

Even though menuitem[] miarray= (menuitem[]) items.toArray() doesn't work, will this:

Object oa[]= items.toArray();
menuitem mi= (menuitem) oa[0]

If so, what is the subtle distinction I'm missing here? I guess this is academic at this point, since what you gave fixed my problem, but it is curious.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Agreed. It is a subtle distinction. You just have to remember that an array is simply an object that holds objects -- it's type doesn't change based on what it is holding.

In fact, let me modify your third example, to return the equivalent of what the toArray() method is returning.



The items in the array IS-A menu item object, and hence, can be casted. However, the array is still an Object array, and hence, can't be casted.

Henry
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
Gotcha! Thanks for the patience, and the education!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ClassCastException in Tag Library
 
Similar Threads
JMenuItem
f:selectItems can't set value
Adding a GUI to an existing program
Menu Bar
Custom Tags & Custom Types