• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

question on getClass and instanceof

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Marshal
Posts: 77157
370
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't getClass() more like, “tell me your name,” and instanceof is, “are you Campbell Ritchie or his descendant?”
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic