🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8133

How to test if object has an exact type

#8133
From: David Crocker
Date: Fri, 20 Apr 2001 13:24
20 lines
1857 bytes
I have a requirement to distinguish at run-time whether a variable X is
exactly of some type T as opposed to being of some type derived from T. I
have to assume that T might be extended by an ignorant third party, so
defining a virtual function in T and then overridding it in all descendents
of T is not an option.

What is the best way of doing this? I know I can use getClass() to get the
Class object of the run-time type of X, but is there an efficient way to get
the Class object that corresponds to T? So far the best I can come up with
is to declare within class T the following:

   static const Class myClass = Class.forName("T");

then I can express the test as "X.getClass() == T.myClass".

Is there a better way? Does Java provide any other way of obtaining the
Class object for a given type? Any other ideas or advice will be
appreciated.

Dave Crocker, Escher Technologies Ltd.
Technical Details
Message-ID: <3ae038b6$1_2@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!newsfeed.google.com!sn-xit-03!supernews.com!hermes2.visi.com!news-out.visi.com!news-out.visi.com!hermes.visi.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,6c5d8e5267379055,start
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-04-20 06:44:13 PST
From: "David Crocker" <dcrocker@imsltd.com>
Newsgroups: borland.public.jbuilder.java.language
Subject: How to test if object has an exact type
Date: Fri, 20 Apr 2001 14:24:57 +0100
Lines: 22
Organization: Intelligent Micro Software Ltd.
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
NNTP-Posting-Host: 194.202.27.87
Message-ID: <3ae038b6$1_2@dnews>
X-Trace: dnews 987773110 194.202.27.87 (20 Apr 2001 06:25:10 -0700)
Path: arc1-ca.pugleaf.net!supernews.google.com!newsfeed.google.com!sn-xit-03!supernews.com!hermes2.visi.com!news-out.visi.com!news-out.visi.com!hermes.visi.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews