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 question on getClass and instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question on getClass and instanceof" Watch "question on getClass and instanceof" New topic
Author

question on getClass and instanceof

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
Hi friends

I have a question please (just for the sake of understanding!)... Suppose that I have an ArrayList that holds any type of object... and that I will not know in advance what sort of objects it holds,

How can I automate the code below? Notice that I put "???" in the code because I don't know what the class is! Is there a way to find out automatically what the class is?
I know that Object.getClass() will return the class... but I don't know how to use it to make the code automatic!



At the moment there is no reason why I want to do something like this... I just want to understand how things work... ... I appreciate your patience!

Thanks
HannaH
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

H Melua wrote:
I have a question please (just for the sake of understanding!)... Suppose that I have an ArrayList that holds any type of object... and that I will not know in advance what sort of objects it holds,

How can I automate the code below? Notice that I put "???" in the code because I don't know what the class is! Is there a way to find out automatically what the class is?
I know that Object.getClass() will return the class... but I don't know how to use it to make the code automatic!



At the moment there is no reason why I want to do something like this... I just want to understand how things work... ... I appreciate your patience!


I think that you are looking at it wrong...



The purpose of the instanceof isn't to handle many different types -- actually, maybe it is, but it is from the code point of view. There is something in the "if" condition block that requires that the instanceof condition be passed to enabled it to work.

So, the question is NOT ...

"Suppose that I have an ArrayList that holds any type of object... and that I will not know in advance what sort of objects it holds", how to deal with it with instanceof?

But the question should be ...

"Suppose that I have an ridiculous amount of code that is needed in handling all different types of objects" ... and quite frankly, if you need an endless amount of code, it is not the instanceof that is an issue.

Henry
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

To provide a more conceptual answer:
Think of getClass() as asking "Are you H Melua"?
This will give you the precise object type.

Think of instanceof "Are you H Melua's son/daughter?"
This will give you the relative object type (if the object implements or extends a given type)

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Isn't getClass() more like, “tell me your name,” and instanceof is, “are you Campbell Ritchie or his descendant?”
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Campbell Ritchie wrote:Isn't getClass() more like, “tell me your name,” and instanceof is, “are you Campbell Ritchie or his descendant?”

Come to think of it yes. My analogy was not precise. I stand corrected. Thanks Campbell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on getClass and instanceof
 
Similar Threads
Swimming up Stream
instanceoff
Convert String in Date
Casting doubt