Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

dynamic type

Thread view: 
studentoflife77@gmail.com - 15 Nov 2007 23:21 GMT
Hello all,

a primitive question

I receive an object var which is of the type Object
then to verify its type i write following code

if (var instance of X)
... do something
if (var instance of Y)
... do something
if (var instance of Z)
... do something
if (var instance of J)
... do something

With Java Generics can i find out the dynamic type of var and typecast
it to its dynamic type, this will save me these unnecessary lines of
code ?

Thanks
Student
Daniel Pitts - 15 Nov 2007 23:38 GMT
> Hello all,
>
[quoted text clipped - 18 lines]
> Thanks
> Student
Unfortunately, no.
however, if you have a lot of "if instanceof" chains, you probably
should look into making this object polymorphic instead.  It'll produce
cleaner code, and let X,Y,Z, and J decide on how they want to handle "do
something"

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

studentoflife77@gmail.com - 16 Nov 2007 00:40 GMT
I agree with you. Unfortunately I am using a library given by some
other team in which I have to check instanceof for 7 different classes
and then call the same getXX() method. i dont know why they didn't
pull this method into the base class.

The only reason i can think of is that in their hierarchy there must
be more than 7 sub classes from the same base class and the other
classes (apart from the 7 having the method) may didn't require the
same getXX() method

thanks
Student

On Nov 15, 6:38 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> studentoflif...@gmail.com wrote:
> > Hello all,
[quoted text clipped - 28 lines]
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Daniel Pitts - 16 Nov 2007 01:13 GMT
[some top posted reply, fixed below]
Please don't top post.
> On Nov 15, 6:38 pm, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
[quoted text clipped - 33 lines]
>> thanks
>> Student

My suggestion is to ask the other team to add that method to a base
class or at least to an interface.  If you can't the other team to make
changes, than they aren't your friends :-)

If you are feeling particularly proactive, you might even suggest to the
other team that you can make the change yourself.
Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.