| Author |
Dynamic type casting for Java Objects
|
bhaswar goswami
Greenhorn
Joined: Jul 04, 2005
Posts: 14
|
|
Hi, I have a situation where i need to cast java objects dynamically . saying : public void getObject( Object paramObject , String type) { ArryList list =( type )paramObject ; // This type i wil know dynamically while calling the Method. } can i do it in Java. If yes . How ?? NB: i don't want to put different if blocks for ddifferent Java types.
|
Regards,
Bhaswar Goswami
SCJP 5.0 , SCDJWS
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Yes, you can do it, but if you are really using anything from the Collections framework, which includes ArrayList, then you want to avoid casts like the plague. An incorrect cast will cause your entire application to crash. You would need to set your ArrayList up as a generic List; see "Generics" in the Java tutorial, in the first instance.
|
 |
magesh karnam
Greenhorn
Joined: Sep 08, 2003
Posts: 14
|
|
Use reflection and you can do this. What is reflection Thanks Magesh
|
 |
ak pillai
author
Ranch Hand
Joined: Feb 11, 2006
Posts: 288
|
|
Also can try "instanceOf" operator prior to casting. Care should be taken not to use "instanceOf" operator in large loops etc since it can adversely affect performance. If you use reflection remember to cache reflected values otherwise it can adversely affect performance.
|
java j2ee job interview questions with answers | Learn the core concepts and the key areas
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by bhaswar goswami: I have a situation where i need to cast java objects dynamically .
With all due respect, I seriously doubt that you are.
ArryList list =( type )paramObject ; // This type i wil know dynamically while calling the Method.
You could simply cast to ArrayList. Why would you want to cast to something else?
|
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
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
I have a situation where i need to cast java objects dynamically .
I agree with Ilja that this is something that you do not want or need to do. - You write the source code before you compile or run the program. - You say that you don't know what you want to cast the variable to before runtime. Those two are in conflict with eachother. At the moment you write the source code, you already know to what type of variable you are assigning the result of the cast expression to - you already know it at compile time, not at runtime. So you must be confusing some things. Can you explain why you think you need to do this? [ July 18, 2006: Message edited by: Jesper Young ]
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
|
|
Care should be taken not to use "instanceOf" operator in large loops etc since it can adversely affect performance.
Whoa! where did that come from? The instanceOf operator is used (invisibly) every time a reference cast is done so it is very frequent in Java programs and has to be fast. String thing = (String) object ; // there is an instanceOf check being done - otherwise how would you get a class cast exception?? Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: Dynamic type casting for Java Objects
|
|
|