4

So I realize there is a toString() method from the Object class. However how do I set a fromString() method?

public String toString() {
    return "";
}

What's the point of having toString() and not fromString()? Or am I just not able to find it? To clarify I am trying to use Gson and it keeps converting my object to its string representation instead of as the object.

pb2q
  • 58,613
  • 19
  • 146
  • 147
ThatGuy343
  • 2,354
  • 3
  • 30
  • 55
  • 4
    `toString` provides a means to generate a "description" of the object which can be helpful when you want to quickly dump the object values, mostly when you are debugging. `fromString(String)` doesn't exist, you would need to create a constructor or static factory method. Java came with object seralisation, but now days, something like [JAXB](https://docs.oracle.com/javase/tutorial/jaxb/intro/) would generally be a better solution for saving/loading objects – MadProgrammer Aug 17 '15 at 04:38
  • 1
    There is no contract which says that the `String` output from `toString()` has to convertible back into the Object whence it came. As the @MadProgrammer mentioned, you can look into serialization if you wish. – Tim Biegeleisen Aug 17 '15 at 04:39
  • Not every object can be constructed from a string, but every object can be represented with a string. That's why every object has a toString method and very few have fromString methods. Also, even for classes which could be built from a string, not every string could be coerced into an object of that class. How would you convert the string "amphibian" to an Integer? – Carl Manaster Aug 17 '15 at 04:39

4 Answers4

5

Note the following from the documentation for toString:

The result should be a concise but informative representation that is easy for a person to read.

This method is less than functional: it's used to present human-readable information, useful i.e. for debugging.

A more purpose-driven API, which seems to be what you're suggesting, will be pretty context-specific. Often you'll see something similar to a fromString, see for instance the various parse methods.

The closest that we have to a general approach is the object serialization and de-serialization API (tutorial here, docs here), which is still left open for specific implementation:

The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

None of this precludes you from writing your own fromString, but it sounds like you're having a specific problem with the Gson libraries. Look at the various fromJson overloads, starting here.

pb2q
  • 58,613
  • 19
  • 146
  • 147
1

It would be hard to implement. It seems logical that fromString(object.toString()) should reconstruct the object. Then, the toString() then would have to have all information needed to initialize that class, instead of just the important stuff.

For example, consider an ArrayList. Its toString() gives only its contents, which seems fine and useful until your try to initialize it…you'd need to properly set its capacity, for example, but you don't have access to it from its toString(). But you wouldn't want to see that in its toString() for encapsulation reasons.

saagarjha
  • 2,221
  • 1
  • 20
  • 37
  • Worse yet: is `"[123, 456]"` a list of Integers? Longs? Strings (e.g. `list.add("123")`)? For fromString to be able to reconstruct the list, that string representation would have to be a _lot_ more verbose, which would make it much less helpful for human-readable formats like logs. – yshavit Aug 17 '15 at 19:02
1

There is no such method called fromString(), you need to implement it at your own. The logical reason seems to be simple, that one would always need to have it implemented as per the requirement.

Also toString() method provides you a generic way to somehow represent and convey some useful information about the object. You can override it and provide your own implementation if you need a different behavior than the default one. On the other hand it is not a generic requirement to convert every String into an Object representation, hence fromString() method does not exist.

Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42
1

toString() is intended as a way to make it so your own custom objects can be included in text output using println() etc.

System.out.println("The value of my object is " + myObject);

Most of the time you do not output enough information in a toString() to be able to recreate the object later. It is commonly just a summary of your object.

Converting an object to and from a non-binary format is called "serialization" and "deserialization" respectively. You can read more about that here:

Java Serializable Object to Byte Array

Community
  • 1
  • 1
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181