File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Casting with a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting with a String" Watch "Casting with a String" New topic
Author

Casting with a String

Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
Probably a bad title, but best I could do.
OK, suppose you have a class, like GamePiece and then subclasses of that class like Knight, Rook, Queen, King, etc. And suppose you have an array of GamePieces....some of them will be Knights, some Rooks, some Queens, etc. You are going through the array, and you don't know which is which so you are pulling them all out as Gamepieces, but each Gamepiece is self aware of what it actually is so you can pull it out as a GamePiece and then suppose you can call whatAmI on that piece and it returns a String "Knight". How can you use this to cast it to a Knight. I'd be interested in hearing if there is a better way to handle this situation, and I'd also be interested in the answer to this question, whether there are better ways or not. So, specifically, can you create an instance of a class using a String that contains the name of the class?

With Respect,
Matt
Saif Ullah
Greenhorn

Joined: Mar 21, 2002
Posts: 29
I think, java's object oriented approach can server better than using String objects. Try to create simple classes for Knights, Queens, Rocks, then use them in the array, or you can also use utility Collection classes, for effecient search purposes, Thus it becomes easy to cast and recognize each gamepiece. I hope you got the point.
A Java Student
Saif Ullah
SCJP2
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
Yes, I get the point...I think perhaps I did not explain myself clearly, because I was meaning that you have made classes for KNight, Rook, Queen, etc.
Forget all about my example...can you answer me this? Suppose I have an Object and want to cast it to another class say Knight.
Object o;
Knight k;
k = (Knight)o;
Or something like that (assuming that they could be cast).
But suppose you don't know what you are going to be casting o to. You have a String say s = "Knight". You don't want to make a switch statement or whatever everytime. Is it possible to cast to a class using a String to do the cast? Is there some way using class Class or something like that where you can pass a string "Knight"? I'm not asking if it's a good idea, I'm asking if it is possible.

With Respect,
Matt
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Matt, why don't you just make use of polymorphism? Can you please provide an example of why you think you need to cast to the more specific class? I suspect there might be another way...


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
Saif Ullah
Greenhorn

Joined: Mar 21, 2002
Posts: 29
I think, Ilga preuss, also thinking according to the idea, I have presented, this will be more effective, and effecient, If you use, polymorphism the unknow objects will be known, at compile time, due to polymorphic behaviour, you will use an custom define interface, like GamePiece, then implement each new class with this interface, with names, like Knights, Queens, Rocks, thus you got the knowledge of unknown object at the casting time, it will help you,
If you still got problem try to write your program, so that I could get it, and try to understand what exactly you want to do.
Please don't be so persistent to Strings, try other behaviours (~_~)

A Java Student
Saif Ullah
SCJP2
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
I have a firm understanding of polymorphism. You are thinking too much about my first example. My question still stands:
Is it possible to create an instance of a class using a String? Again, I'm not asking if it is a good idea or not, or if there is a better way. I am asking if it is possible.
With Respect,
Matt
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
For the record, I realize that's it's NOT a good idea, and that there ARE better ways. I am wondering if it is possible.
With Respect,
Matt
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
Yes, it can be done with Class.forName(String).
I used a bad example in the first post, one that, as has been pointed out, would call for polymorphism.

With Respect,
Matt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You seem to have two slightly different questions here. If an instance has already been created, and you're just trying to cast it to a more specific type than what the compiler currently thinks it is, then (aside from better use of polymorphism) your best best is probably something like:

If, alternately, you want to create a new instance knowing only the class name, yes, use reflection:Or something like that.
[ July 02, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting with a String