Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

isInstance

 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
What is the difference between instanceOf and isInstance method.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can say

someObject.getClass().isInstance( x )

when you don't know what class someObject is at compile time. You must know what class you're comparing at compile time when you say

x instanceOf SomeHardCodedClass
 
Joshua Bloch
Author and "Sun God"
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the name of the operator is instanceof (note the capitalization).

Regards,

Josh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic