This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances" Watch "FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances" New topic
Author

FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances

Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Pre-emptive apologies to all who knew this answer but ...
Just as FYI and to save anyone else the time to validate this themselves. I needed to determine if isAssignableFrom() and isInstance() took into account whether the subject classes (and objects) were loaded by different ClassLoader instances. The simple answer is yes. The same class loaded by two different ClassLoader instances will return FALSE for isAssignableFrom().

This is the code I used to validate it (three classes):


Resulting output is:


Loading 'us.happycat.classloader.JustAClass'
Loading 'java.lang.Object'
Loading 'us.happycat.classloader.JustAClass'
Loading 'java.lang.Object'
cl1 is us.happycat.classloader.SimpleClassLoader@42719c
cl2 is us.happycat.classloader.SimpleClassLoader@422ede
clazz1 is NOT assignable from clazz2
clazz2 is NOT assignable from clazz1
i1 is instance of clazz1
i2 is instance of clazz2
i1 is NOT instance of clazz2
i2 is NOT instance of clazz1
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances