Article View: borland.public.jbuilder.java.language
Article #8134Re: How to test if object has an exact type
From: Torsten Welches
Date: Fri, 20 Apr 2001 13:37
Date: Fri, 20 Apr 2001 13:37
33 lines
2175 bytes
2175 bytes
David,
try that:
String s = "TEST";
boolean isFalse = s.getClass().equals(Object.class);
boolean isTrue = s.getClass().equals(String.class);
Torsten
"David Crocker" <dcrocker@imsltd.com> wrote:
>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:
<3ae03b95$1_2@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!cyclone2.usenetserver.com!news-out.usenetserver.com!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3ae038b6$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,6c5d8e5267379055 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-20 06:44:13 PST From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Subject: Re: How to test if object has an exact type Newsgroups: borland.public.jbuilder.java.language X-User-Info: 193.158.55.74 193.158.55.74 References: <3ae038b6$1_2@dnews> NNTP-Posting-Host: 207.105.83.65 Message-ID: <3ae03b95$1_2@dnews> Date: 20 Apr 2001 06:37:25 -0700 X-Trace: dnews 987773845 207.105.83.65 (20 Apr 2001 06:37:25 -0700) Lines: 34 Path: arc1-ca.pugleaf.net!supernews.google.com!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!cyclone2.usenetserver.com!news-out.usenetserver.com!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews