Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using the instanceof operator

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering if it is possible to use the instance of operator when the second argument will not be known at compile time. Using an actually instance of an object perhaps to retrieve the type of argument that instance of wants. like this:

Pseudocode:


That type of thing doable?

Thank you,

tjcr
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did happen, when you compiled and (if it compiled) executed your example?

You might also want to check out http://mindprod.com/jgloss/instanceof.html.

Regards,
Uli
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't possible, but java.lang.Class has a method called isInstance. You should use that.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't that be the same as



That wouldn't work for interfaces and subclasses, though.
[ December 03, 2008: Message edited by: Ulf Dittmer ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:



That wouldn't work for interfaces and subclasses, though.


Yes and no. If you load the same class via two ClassLoaders, you can end up with objects for which the above is true, but object1.getClass() == object2.getClass() is false; Rob's "isInstance()" will correctly return false in that case.
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes and no. If you load the same class via two ClassLoaders, you can end up with objects for which the above is true, but object1.getClass() == object2.getClass() is false; Rob's "isInstance()" will correctly return false in that case.


Ernest,

Why would this be "correctly" false? I would think that you would want this to return true if they are truly the same class...

Is it possible that if two different class loaders where used then they may have two classes who's implementations are very different but they have the same fully qualified class names? If that's the case then I could see why you would want this to return false. Otherwise I don't understand if the point of the exercise is to discover whether one class is the same as the other.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tristan,

It's correctly false because the JVM identifies a class not just by the FQN but by the [FQN, ClassLoader] combination. If you have a class like this:



Load it via two classloaders, create an instance from each of thsoe two loaded classes, and then call test() on one object passing the other as an argument, you'll get a ClassCastException. Even if they both came from the same class file.

This kind of thing is not just academic; itcomes up occasionally in Web and J2EE apps that run in containers that use ClassLoaders to partition the JVM.
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Hi Tristan,

It's correctly false because the JVM identifies a class not just by the FQN but by the [FQN, ClassLoader] combination. If you have a class like this:



Load it via two classloaders, create an instance from each of thsoe two loaded classes, and then call test() on one object passing the other as an argument, you'll get a ClassCastException. Even if they both came from the same class file.

This kind of thing is not just academic; it comes up occasionally in Web and J2EE apps that run in containers that use ClassLoaders to partition the JVM.


Thank you very much for all the replies.

starting with something like my first post, getting a solid answer from Rob and then learning something deep about java like [FQN, ClassLoader] system for similarity is why I love this place.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic