File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Dynamic type casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamic type casting" Watch "Dynamic type casting" New topic
Author

Dynamic type casting

Frank Lin
Ranch Hand

Joined: Jan 26, 2001
Posts: 76
In the snippet code below
Object obj = null;
String ss = new String("test");
obj = ss;
String ss2 = (String) obj;
^
Where ^ denote the type casted to. Is there anyway to provide the type at run time dynamically?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Frank,
Yep. It's called reflection. Look at the API documentation on class Class. There is a method named getClass() in Object that returns an instance of Class. The Class class has methods to create new instances; discover properties, constructors, and methods; get super classes; etc.
Of course this is a whole lot tougher coding than a simple typecast .
Hope this helps
Michael Morris
SCJP2


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I can't think of any reason to need a "dynamic type cast". Could you please explain what you want to do?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bill william
Ranch Hand

Joined: Apr 14, 2002
Posts: 42
Originally posted by Ilja Preuss:
I can't think of any reason to need a "dynamic type cast". Could you please explain what you want to do?

I have been working on a project which can convert between an XML string and java class. I need to dynamically cast xml node names into a java class if it is an object.
[ April 14, 2002: Message edited by: bill william1 ]
Frank Lin
Ranch Hand

Joined: Jan 26, 2001
Posts: 76
Originally posted by Ilja Preuss:
I can't think of any reason to need a "dynamic type cast". Could you please explain what you want to do?

My question originated from a need to invoke a entity bean dynamicly. As you know, a homeinterface.findByPrimaryKey() returns a remote interface that requires a explicit type cast. What if which bean should be invoked can only be known at run time? Using reflection can be a alternative. Anything else?
mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Hello Morris & Fin,
Can you please tell me how is it possible using Reflection. Using getClass() you can get a "Class". With this class object we can
1. Create a instance of the object represented by the class using class.newInstance()
2. or get the name of the class using class.getName(). But this string containing the name cannot be substituted for casting.
Say,
A = "String"
String str = (A)obj;

I cannot do as above.

Mohamed Zafer
[ April 15, 2002: Message edited by: mohamed zafer ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Frank Lin:

My question originated from a need to invoke a entity bean dynamicly. As you know, a homeinterface.findByPrimaryKey() returns a remote interface that requires a explicit type cast. What if which bean should be invoked can only be known at run time? Using reflection can be a alternative. Anything else?

I don't know wether there are any pitfalls when using EJBs, but the "normal OO solution" would be to use polymorphism for this. That is, let all appropriate classes implement a common interface, cast to this interface and call a method of the interface. Something along the lines of:

Hope this helps, Ilja
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by mohamed zafer:
A = "String"
String str = (A)obj;

I cannot do as above.

Why would you want to??? Casting to anything other than String would be pointless, wouldn't it?
mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Hello Preuss,
Sorry , the statement should have been like this,
A = "String"
A str = (A)obj;
where A can represent any Class.
Mohamed Zafer
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by mohamed zafer:
Sorry , the statement should have been like this,
A = "String"
A str = (A)obj;
where A can represent any Class.

What would you like to do with str afterwards?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Dynamic type casting
 
Similar Threads
ArrayStoreException
adding objects to Combo Boxes in SWT
PLZ tell me ,what's wrong in it????
Namespace question
q on garbage collection