• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Should one recommend to override toString() or not?

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Marshal
Posts: 66505
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 39655
785
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether to override is a technical discussion not a policy one. Maybe we should move this thread to one of the technical forums?
 
Campbell Ritchie
Marshal
Posts: 66505
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trailboss
Posts: 23036
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!