How to cast a String to an object reference?

S

Shawn

Dear All,

I have a Person class already. Now I have a String:

public class Person()
{
void sayHello()
{
System.out.println("Hello");
}

}

Person goodGuy = new Person();

String str = "goodGuy";

((Person)str).sayHello(); //Error: cannot cast from String to Person

This is desireable or I am just totally out of my mind?
 
J

Jeffrey Schwab

Shawn said:
Dear All,

I have a Person class already. Now I have a String:

public class Person()
{
void sayHello()
{
System.out.println("Hello");
}

}

Person goodGuy = new Person();

String str = "goodGuy";

((Person)str).sayHello(); //Error: cannot cast from String to Person

This is desireable or I am just totally out of my mind?

If you're calling a method that really doesn't depend on the underlying
object, just make the method static and don't bother with the object at
all: Person.sayHello().

If the method might depend on the object, you have to get an object of
the right type. You can't just cast any object to any type; i.e., there
is no Java equivalent of C-style casts, or of C++ reinterpret_cast.

Chances are good that if you feel the need to add new methods to
existing objects, you can get what you want by wrapping the objects in a
new type. This also has the advantage that when you no longer need the
extra functionality, you can unwrap the objects, so the extra complexity
is limited to (e.g.) the parsing stage of your compiler, or the
loadUserPreferences step of your application, or whatever. For example,
if you feel the need to call a Person method on a String, try making
Person an interface, and move the functionality into a wrapper class
that knows about Strings.

public interface Person {
public void sayHello();
}

public class PersonableWrapper<T> implements Person {

private T wrappedObject;

public PersonableWrapper(T objectToWrap) {
this.wrappedObject = objectToWrap;
}

public void sayHello() {
System.out.println(wrappedObject.toString() + ": Hello!");
}
}

public class Main {

private static void workWithPerson(Person person) {
person.sayHello();
}

public static void main(String[] args){

Integer i = new Integer(42);
String s = "six times nine";

workWithPerson(new PersonableWrapper<Integer>(i));
workWithPerson(new PersonableWrapper<String>(s));
workWithPerson(new PersonableWrapper<String>("goodGuy"));
}
}
 
O

Oliver Wong

Shawn said:
Dear All,

I have a Person class already. Now I have a String:

public class Person()
{
void sayHello()
{
System.out.println("Hello");
}

}

Person goodGuy = new Person();

String str = "goodGuy";

((Person)str).sayHello(); //Error: cannot cast from String to Person

This is desireable or I am just totally out of my mind?

It is desirable that the above attempt at casting generate a
compile-time error. Given that Person and String are not related via
inheritance, there is no way for this cast to succeed, and rather let you
find out at runtime, the compiler is able to detect this and tell you so at
compile time.

- Oliver
 
S

Shawn

Thank you all. What I really need is:

public interface Person()
{
public void saySomething();
}


Person goodGuy = new Person(){
public void saySomething() {
System.out.println("How are you?);
}
};

Person badGuy = new Person() {
public void saySomething() {
System.out.println("I am a bad guy");
}
};

String str;

//in the middle of my program, str gets a value(goodGuy, badGuy etc). If
the value is "goodGuy", "How are you?" was printed out; if the value is
"badGuy", something else was printed out.

I could use if ... else if ... to do the job. But if there are 10
instances of interface Person(now str can be assigned 10 possible
values), if block will be very long.

I am seeking a better way to do this job.

Thank you again for all your help. I greatly appreciate it.
 
R

Robert Klemme

Thank you all. What I really need is:

public interface Person()
{
public void saySomething();
}


Person goodGuy = new Person(){
public void saySomething() {
System.out.println("How are you?);
}
};

Person badGuy = new Person() {
public void saySomething() {
System.out.println("I am a bad guy");
}
};

String str;

//in the middle of my program, str gets a value(goodGuy, badGuy etc). If
the value is "goodGuy", "How are you?" was printed out; if the value is
"badGuy", something else was printed out.

I could use if ... else if ... to do the job. But if there are 10
instances of interface Person(now str can be assigned 10 possible
values), if block will be very long.

I am seeking a better way to do this job.

Use a Map.

robert
 
T

Thomas Weidenfeller

Shawn said:
> //in the middle of my program, str gets a value(goodGuy, badGuy etc). If
the value is "goodGuy", "How are you?" was printed out; if the value is
"badGuy", something else was printed out.

I could use if ... else if ... to do the job. But if there are 10
instances of interface Person(now str can be assigned 10 possible
values), if block will be very long.

I am seeking a better way to do this job.

Maybe a Map, maybe a state machine, or whatever. Since you are not
telling us what you want to achieve, why you want to do this, it is
difficult to tell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top