The Artima Developer Community
Sponsored Link

Java Answers Forum
dinamyc cast to an object

3 replies on 1 page. Most recent reply: May 19, 2004 7:12 AM by Friendly

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Friendly

Posts: 2
Nickname: friendly
Registered: May, 2004

dinamyc cast to an object Posted: May 19, 2004 2:50 AM
Reply to this message Reply
Advertisement
I need to do cast on an object and to transform it to a type, determined by the value of a variable.
But when I do cast gives an error me, it says to me that it is not a type.
My code:

newType = //read the type of a file
c = Class.forName(nClase);
cons = c.getConstructors();

//I NEED TO MAKE a DYNAMIC CAST NOW, MUST BE ABLE TO CHOOSE the OBJECT CLASS THAT IS

obj = (newType) cons[0].newInstance(aux);
// I WANT THAT IT TAKES THE VALUE OF THE VARIABLE, THAT IT IS A TYPE. BUT HE DOES NOT DO IT.

SOMEBODY COULD SAVE THE LIFE TO ME. THANKS; -)



sorry for my bad english


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: dinamyc cast to an object Posted: May 19, 2004 5:34 AM
Reply to this message Reply
Why not use an if-else structure?

newType = //read the type of a file 
 
if(newType == someValue)
   obj = (Type1)...
else if(newType == otherValue)
   obj = (Type2)...

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: dinamyc cast to an object Posted: May 19, 2004 6:10 AM
Reply to this message Reply
The object must either inherit in some way from the class or type you want to cast it to. Otherwise you get a cast exception.

On Line reference for casting is at:
http://java.sun.com/docs/books/jls/first_edition/html/5.doc.html#20232


Example:

Vector v = new Vector;

The Vector class implements several interfaces, each of which you can cast a Vector object to.

Cloneable c = (Cloneable) v;
Collection c2 = (Collection ) v;
List list = (List) v;
RandomAccess ra = (Cloneable) v;
Serializable s = (Cloneable) v;

Stack is a subclass of Vector.
The following will cause an error, because every Vector object is NOT a Stack. (You can't "down-cast".)
Stack stack = (Stack)v;

BUT

every Stack is a Vector because Stack inherits FROM Vector.

So, if:

Stack stack = new Stack();

you can cast a Stack object into a Vector object like:

Vector vector = (Stack)stack;

In this case you can "up-cast".

Figure out what all this means and I am sure you can write some if then else blocks to do the job and get it done correctly.

Also look at this reference:
http://java.sun.com/docs/books/jls/first_edition/html/15.doc.html#80289

which explains using instanceof

Using instanceof would make your if then else block work correctly.
if (object instanceof Stack){
   newObject = (Stack)object;
}else if (object instanceof Vector){
   newObject = (Vector)object;
}else{
   //default or error code
}
 

Friendly

Posts: 2
Nickname: friendly
Registered: May, 2004

Re: dinamyc cast to an object Posted: May 19, 2004 7:12 AM
Reply to this message Reply
Thanks to everybody.

I didn't want use if-else because i have many types.
But i think i must use it. Because the cast is checked at compile time and the variable get value at run time. So the dinamyc cast is impossible.

Thanks again. ;-)

Flat View: This topic has 3 replies on 1 page
Topic: how to create rollover button in java? Previous Topic   Next Topic Topic: Problem with dynamic class generation...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use