As the name says, what it does is it results in a text that expresses what that object is, so every object has this method.
There is some controversy about the right conceptual use.
My understanding, and many much more experienced people and with training (in the broad sense, not from certified study only) much better than I, understand that this is a debugging function or at most for converting data to serialization or some very specific activity that has more to do with the mechanism than with the business rule.
There are those who disagree with that, and in Java there seems to be a culture of being more than that, which shows a strange culture because it’s a language that tries to do the right thing, even if it’s not always practical, and this along with exceptions and other things in language, seek the most practical even if conceptually wrong.
There are still those who think they can do anything on it, can even write on the console or on a GUI screen. This I think that everyone who understands something about software development agrees that it is abuse, at least conceptually. And everything that is conceptually wrong in real, complex code one day explodes in your face. AP code is right at this point.
But from the previous concept he’s wrong because he’s defining business rule, it’s resulting in something to be used in the application, probably with user interaction, which is not the goal of toString()
.
This method should only return the simple identity of the object. Anything that indicates what this object is, what its important content is as basic as possible.
If you want a formatted message, create a method to return it. If you want to use something standardized, you can even use a toString(alguma coisa)
(so with a different signature of what is in every object) which is already something that is not used to meet the needs of the mechanism. But it can be another method as well. Java does not have a standardization for this. Java was created at a time when much was not yet perceived as wrong.
Of course in the example used is not something so critical because it does a little what it needs, but it has a lot of penduricalho.
Let’s get the documentation provided by Math in his reply in another question:
Returns a string representation of the Object. In general, the toString method Returns a string that "textually represents" this Object. The result should be a Concise but informative representation that is easy for a person to read. It is Recommended that all subclasses override this method.
Is this form representative of the object? Is it concise? I don’t think so, until I answered there. Java actually encourages right, but many people don’t interpret it that way in the documentation. So I strongly disagree with Math’s response there.
Has more:
The toString method for class Object Returns a string consisting of the name of the class of which the Object is an instance, the at-Sign Character `@', and the unsigned hexadecimal representation of the hash code of the Object. In other words, this method Returns a string Equal to the value of:
It says you should use the class name. Of course giving some identity value is more useful than just the name, it will have advantages. The default implementation of this method in most classes is like this, returns the class name and not the object identity (understand the difference). It is for purification purposes. Even a reflection (good) of poor.
Think, if one day you need to return a different text, what will you do? Change this text? What if the application depends on that specific text to do something? After all the method is mechanism, it can be used for anything involving representation and it should be stable, the result is part of the contract of API. And if you really need to change, you have to create another method, it would become a mess because it’s mixing functions.
Adorn the text of toString()
was the right Integer
it should be like this:
public String toString() {
return String.format("O valor do objeto é %s", String.valueOf(value);
}
That’s what you’re doing in the question class. That’s very wrong.
I prefer it this way:
public class comp {
private String nome;
private Data aniversario;
public comp (String n, Data a) {
nome = n;
aniversario = a;
}
public String toString() {
return String.format("%s - %s", nome, aniversario);
}
public String getTextual() {
return String.format("O teu nome é %s , e a fazes anos no dia %s", nome, aniversario);
}
}
I put in the Github for future reference.
Maybe it could even be: String.format(nome);
, I don’t think too much birthday is part of the identity. The secondary method was just a simple example, in real application I don’t even know if I would do it, I don’t even know if there would be something like that in the object, I think it can be a business rule pluggable, but that’s another matter.
Possible duplicate of Doubts about the toString() method of the Object class
– user28595
Related: What’s the difference between using Object.toString() and String.valueOf()?
– viana
I almost found duplicate, but then I saw that the focus there is another.
– Maniero