aspose file tools*
The moose likes Ranch Office and the fly likes Should one recommend to override toString() or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Should one recommend to override toString() or not?" Watch "Should one recommend to override toString() or not?" New topic
Author

Should one recommend to override toString() or not?

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
From the perspective of learning about the language, here in the "Beginning Java" forum, I wanted to make sure the OP did not think that overriding toString() was the only way to use System.out.println(), or even that it was the preferred or common way. That would be one interpretation of what you are saying.

I said that you would not have to override toString() in this case; perhaps I should have been more explicit and said "You would not have to override toString() to produce the specific output you want".

It is also arguable that your suggestion for "toString()" does not "textually represent" the BankAccount object. Depending on the needs of the application, just returning the account number *might* be fine, but might not be enough, and might be the wrong string, etc.

But we can answer the OP's question without going into all that -- he/she wanted to know about using println to print the account number. The question implies that he does not understand how to extract field values from an object to have them printed by println(). Yours is one way to do that, and mine is another perfectly legitimate (and more flexible) way to do that.

The OP was not asking about overriding toString(), and I think it is misleading to imply that he is supposed to for this purpose. I said nothing about whether he "should" override toString(). I daresay most of the java classes I've seen do not, and I think it is misleading to tell a newish programmer that he should do so to get println output. If later he wants to output the account holder's name as well, but still have this statement work this way, would you tell him to put a flag variable in the class so he can set it so that toString() will give him the 'right' string?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I am going to split this discussion. I think the discussion about whether one should advise people to override toString() ought to be kept separate from suggestions about the original question about how to display an object. I shall also move discussion about what one advises people to the Ranch Office forum, where we usually discuss such things.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

Whether to override is a technical discussion not a policy one. Maybe we should move this thread to one of the technical forums?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
This discussion originated in "beginning Java". I split the discusion because it appeared to me it was turning into a dispute, and we usually move disputes here, to Ranch Office. I formed the opinion there might be a dispute. Now an opinion is an opinion, which is made in view of the evidence available at the time, and may be mistaken. What is more, I thought I had to act quickly. If I was mistaken, I am sorry and apologise.

Ralph Cook told me it appears I am agreeing with Maneesh Godbole because of where I split the discussion. Not so; I split it there because I perceived a dispute arising. I might have been misttaken in that perception. We don't have much experience in using split under such circumstances, because the split feature is quite new. We also don't have the facility to rejoin the threads. Nor could one split a thread in several places, because it would create too many multiple threads.

I agree one ought to override toString(). I agree that the problem posed in that "beginning" thread could be answered differently; in fact I made a suggestion which might have addressed the problem better than overriding toString. But I thought it best to move any possible disputes here. That is why we have this thread in Ranch Office at all.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20532
    ∞

I think any discussion about whether to override toString() belongs in another forum.

Let me say my piece on what the official CodeRanch policy is on an engineering philosophy: I think people have philosophies. Anything that would be an official policy would be my policy. And there are a lot of things that are thought of as "acceptable" by most standards that I find unacceptable. But if I were to push these things, I would probably alienated 90% of the staff an 95% of the existing users. So I don't.

Therefore, people can have their own positions. And people can express their position without taking a baseball bat to somebody else's position (not that this is a problem now - but I just thought I would throw that in there).

So, I'm pretty sure we are now all done with this thread.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Should one recommend to override toString() or not?