Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Dynamic Casting

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All:

I have a situation here that I need to cast an Object type at run time dynamically. I already know the class name is valid and myMethod() and "myString" are all valid. What do I need to fill in, as in the following code...

public method(Object o)
{
className=getClassName(); //I can get this.
///how am I going to use the className into casting?
(fill in here)o).myMethod() = "myString";
}

Thank you.

-RM
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no such thing as "dynamic casting" in Java. For the object to be useful, you have to know something about its type before you load it -- for example, an interface it implements, or a superclass. You can cast to those types. You could use an if-then if you needed to, if there were multiple possibilities.

Remember that casts don't actually do anything -- they just tell the compiler what type an object is; if the information isn't available until runtime, it obviously isn't useful to the compiler.
 
Sheriff
Posts: 26958
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what "myString" means in that code attempt. But let's bypass that and assume that you're saying the class, whatever it is, is guaranteed to have a void method called "myMethod" with no parameters. Then what you do is this:

1. Declare an interface that specifies that method:2. Make the class that you're going to be casting in that code implement that interface:3. Cast the object to the interface before using it:
[ February 08, 2006: Message edited by: Paul Clapham ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic