Article View: borland.public.jbuilder.java.language
Article #7643Re: About class cast.
From: Torsten Welches
Date: Fri, 01 Jun 2001 07:42
Date: Fri, 01 Jun 2001 07:42
29 lines
2116 bytes
2116 bytes
there is no need to cast, instance_Of_myClass will have the proper runtime type "automatically". To verify this try System.out.println(instance_Of_myClass.getClass().getName()); The reference type (compile time) however is Object. You can't simulate the following: MyClass instanceOfMyClass = (MyClass)myClass_list.get(i); Note that the *compile time* type of instanceOfMyClass now is MyClass rather then Object. The runtime type is MyClass in both examples. Casting includes a compile time check and Class.forName() stuff is run-time only. http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#20232 You would probably want the above to work because you need to access members of your instanceOfMyClass. If so, you would need to use reflection to access those members. http://java.sun.com/docs/books/tutorial/reflect/index.html Torsten "wuliao999" no wrote: > String className="com.borland.myClass"; > ArrayList myClass_List; > Object Instance_Of_myClass=myClass_list.get(i); > >Then, how can I cast Instance_of_myClass to myClass ?
Technical Details
Message-ID:
<3b174782$1_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!dnewsReferences:
<3b15f7e6$1_2@dnews> <3b16325b_1@dnews> <3b16f55c$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,25286dd36905df6c X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-06-01 01:02:15 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: About class cast. Newsgroups: borland.public.jbuilder.java.language X-User-Info: 193.158.55.74 193.158.55.74 References: <3b15f7e6$1_2@dnews> <3b16325b_1@dnews> <3b16f55c$1_1@dnews> NNTP-Posting-Host: 207.105.83.65 Message-ID: <3b174782$1_1@dnews> Date: 1 Jun 2001 00:42:58 -0700 X-Trace: dnews 991381378 207.105.83.65 (1 Jun 2001 00:42:58 -0700) Lines: 30 Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!dnews