• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instanceof Operator

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have an object and a string that contains a name of the class...how do I check if that object is an instance of the class specified by that string....for example


what should go into the blank above...please help me
 
Saloon Keeper
Posts: 15529
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe you can use the instanceof operator for this.

Instead, you can try the following:
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
create String Class from fully qualified name of Exception. and use isInstance method of Class to verify wether the argument type can be convert/cast to the reference type. for example see below.



from isInstance method Javadoc:



Determines if the specified <code>Object</code> is assignment-compatible
with the object represented by this <code>Class</code>. This method is
the dynamic equivalent of the Java language <code>instanceof</code>
operator. The method returns <code>true</code> if the specified
<code>Object</code> argument is non-null and can be cast to the
reference type represented by this <code>Class</code> object without
raising a <code>ClassCastException.</code> It returns <code>false</code>
otherwise.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, Stephan beaten me here!
 
Raghavender Ammagari
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic