Article View: borland.public.jbuilder.java.language
Article #8143Re: How to test if object has an exact type
From: Calvin R. Smith
Date: Fri, 20 Apr 2001 20:57
Date: Fri, 20 Apr 2001 20:57
29 lines
2117 bytes
2117 bytes
you can use instanceof with both classes and interfaces to check if the
object is an instance of the class or an implementation of the interface
"David Crocker" <dcrocker@imsltd.com> wrote in message
news:3ae038b6$1_2@dnews...
> 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:
<3ae0a304$1_1@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!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 14:14:04 PST Reply-To: "Calvin R. Smith" <calvinrsmith@hotmail.com> From: "Calvin R. Smith" <calvinrsmith@hotmail.com> Newsgroups: borland.public.jbuilder.java.language References: <3ae038b6$1_2@dnews> Subject: Re: How to test if object has an exact type Date: Fri, 20 Apr 2001 14:57:01 -0600 Lines: 31 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 207.201.104.111 Message-ID: <3ae0a304$1_1@dnews> X-Trace: dnews 987800324 207.201.104.111 (20 Apr 2001 13:58:44 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews