File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to retain type of objects in arraylist ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to retain type of objects in arraylist ?" Watch "How to retain type of objects in arraylist ?" New topic
Author

How to retain type of objects in arraylist ?

deshki raja
Greenhorn

Joined: Apr 03, 2009
Posts: 12
I have unknown objects in my arraylist.....I am able to retrieve the classname of the member objects...but i am unable to use the objects' methods by casting it....can anyone help me...? Is there any other way in which you can hav a list of unknown objects and can use the objects own methods without casting it ? ie, the object retain its type in the list...?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Is there any other way in which you can hav a list of unknown objects and can use the objects own methods without casting it ? ie, the object retain its type in the list...?


First of all, an object "retain its type" regardless of whether it is casted or not... but possible answers to your question.


1. Assuming that the unknown object is from a list of possible types, then you can easily cast and use the methods. Just create a bunch of if-then blocks. that use the instanceof operator to check for type, and cast and call the methods for the different types. Considering that you have a particular method in mind, this unknown type is probably on a known list of types.

2. If it is not from a known list of types, then you can use the reflection classes. You can use the getClass() method to get the Class object. From that you can get the Constructor, Field, Method, etc., objects. Using the Method object, you can introspect and call methods of the instance.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

"jsf new",

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
deshki raja
Greenhorn

Joined: Apr 03, 2009
Posts: 12
Thanks ......I got it working.......
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
If the objects in the list are always of the same type, I would use generics, In fact I can think of very few cases where I have had mixed object types in a list.

Not sure if Generics is a beginner topic, I think it should be, at least in how to use it with collections.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
I think the difference between List, List<Foo>, ArrayList and ArrayList<Foo> probably does count as a beginner's question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retain type of objects in arraylist ?