tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <>
Subject RE: instance of (ot)
Date Thu, 20 Feb 2003 19:35:31 GMT


>  The problem is that I want to test if an object is subclass of a
>java.lang.Class INSTANCE... Nonsense example:

Maybe I'm still not understanding ;)  But why doesn't the instanceof
operator work for you?

Your example:
>  java.util.Date date1 = new java.util.Date();
>  java.sql.Date date2 = new java.sql.Date();
>  Class clasz = date1.getClass();
>  if (date2 instanceof clasz)
>  {
>      do something...
>  }

Why not just do
if(date2 instanceof date1) ...
rather than if(date2 instanceof date1.getClass()) ??

Or are you looking for a comparison that doesn't make subclasses equal?
For example:

Set h1 = new LinkedHashSet();
if(h1 instanceof LinkedHashSet) {
  // This is true

if(h1 instanceof HashSet) {
  // This is true

if(h1 instanceof Set) { 
  // This is true

But you only want the first one?  How about

Object obj1 = new Object1();
Object obj2 = new Object2(); // Object2 extends Object1

if(obj1 instanceof obj1) {
  // This is false

if(obj2 instanceof ob1) {
  // This is true

if(obj2.getClass().getName().equals(obj1.getClass().getName()) {
 // This is false -- is this what you want?

Yoav Shapira
Millennium ChemInformatics

This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message