Home arrow static arrow Java Programming [Archive] - Casting an object whose type is unknown
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Casting an object whose type is unknown
This topic has 6 replies on 1 page.

Posts:5
Registered: 7/24/04
Casting an object whose type is unknown  
Aug 3, 2004 12:55 PM



 
Hi,

I have an array of objects of various types stored in an array of objects Object [ ]. I would like to iterate through Object [ ] and call an instance method on each one. If the method does not exist, a NoSuchMethodException is thrown and dealt with.

Now, before I can call an instance method on an object, I need to cast it. I have tried to cast each object by calling .getClass().getName() on each array element, but this fails to compile properly.

Some advice would be much appreciated.

Thanks

James
 

Posts:31,095
Registered: 4/30/99
Re: Casting an object whose type is unknown  
Aug 3, 2004 12:59 PM (reply 1 of 6)



 
Define that method in an interface (call it X for this post). Have every class that's going to be put into this array implement X. And instead of an array of Object, use an array of X instead.

PC²
 

Posts:24,036
Registered: 2/3/03
Re: Casting an object whose type is unknown  
Aug 3, 2004 12:59 PM (reply 2 of 6)



 
This sounds like a wonky design. Several suggestions:

1) You don't mention the compiler error, so it's tough to troubleshoot that issue.
2) You could use reflection to get at the objects/methods.
3) Better still, you could simply have the objects implement a common interface, saving the whole "various types" problem.

§
 

Posts:13,769
Registered: 00-11-29
Re: Casting an object whose type is unknown  
Aug 3, 2004 1:01 PM (reply 3 of 6)



 
Do you want to cast or do you want to use reflection. They aren't the same thing.

Casting:

String s = (String) objects[0];

The type to cast to can only be defined at compile time. You can't supply runtime arguments to a cast operation.

What exactly are you trying to do?
 

Posts:105
Registered: 6/8/04
Re: Casting an object whose type is unknown  
Aug 3, 2004 1:51 PM (reply 4 of 6)



 
I'm also a bit confused about what you're doing. However, based on what you've said, it seems that trying to just deal with NoSuchMethodExceptions is... not the best way to go about it.

Have you considered using the instanceof ... come to think of it, what's the technical term? Operator? Keyword?

Anyway, let's say that your array of Objects can have either JLabels or JComboBoxes, and you want to set the text of all the JLabels to be "Foo", and ignore the JComboBoxes:

for(int a = 0; a < array.length; a++){        if(array[a] instanceof JLabel)        {                ((JLabel)array[a]).setText("Foo");        }}


This kind of thing might be a good idea, assuming there isn't something really weird in what you're trying to do that I don't understand.
 

Posts:3,258
Registered: 00-08-28
Re: Casting an object whose type is unknown  
Aug 3, 2004 2:24 PM (reply 5 of 6)



 
[url http://www.javaworld.com/javaworld/jw-12-1999/jw-12-performance.html] Cost of casting [/url]
 

Posts:5,965
Registered: 5/17/03
Re: Casting an object whose type is unknown  
Aug 3, 2004 2:37 PM (reply 6 of 6)



 
I have an array of objects of various types stored in
an array of objects Object [ ]. I would like to
iterate through Object [ ] and call an instance method
on each one. If the method does not exist, a
NoSuchMethodException is thrown and dealt with.

Exceptions should be used for exceptional situations and not be part of the normal logic.

The Object class is basically an artificial superclass. Couldn't you have a separate array for each class instead. Then you would know what you have where, and also get the benefit of static type checking.

Instead of an array you could use a List or maybe a Set from the collections.
 
This topic has 6 replies on 1 page.