• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

myClass.toString - reverse

 
A Saari
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

myClass myObject = new myClass();
String s = myObject.toString();

How do I take the String object - s - and get myObject back?


Thanks,
Amy
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to write your own method to do something like that depending on the specifics of your class. A common method signiture for this type of operation is something like:

 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saari,

In the general case, you can't do such thing. toString() is a one-way conversion.

Your Java object generally contains lot's of info, whereas the toString() is just a String representation which contains much less. And this resulting string as no link with the original object.

To use real world analogy, if your object was a house, toString() would be a picture of that house.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic