File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retain type of objects in arraylist ?

 
deshki raja
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"jsf new",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
deshki raja
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ......I got it working.......
 
Gavin Tranter
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the difference between List, List<Foo>, ArrayList and ArrayList<Foo> probably does count as a beginner's question.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic