• 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

using the instanceof operator

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't possible, but java.lang.Class has a method called isInstance. You should use that.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What? What, what, what? What what tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic