• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me understand the toString method  RSS feed

 
Tanya Wilson
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a freshman in a computer science program. Though not very apparent to me it's my second semester of programming with Java. I have never coded or done any programming of any kind before last semester, and I am having such a hard time keeping interest in this Java crap... But that's beside the point! My assignment is to create a Bank program. I've created my BankAccount class and now I'm working on my Bank class before I move on to my Teller class. In Bank I am supposed to use a toString method, but I have absolutely no clue what it is or what it does. A friend in class showed me his code, but I still can't figure out what the heck it's doing. Please help.
 
Daryl Cofer
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, every class that you create is derived from the Object class.
The Object class defines the toString() method. Until overridden, the toString method will return the hashCode for your object.
In your case, you need to override the base (Object) class' toString() method inside your Bank class.

In most instances, the toString() method inside your class will return a String representation of your object.
For instance, if you had a class called Closet that kept track of the number of shirts and pants that was in it, your toString() method might look something like this:

 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation

Tanya Wilson welcome to the Ranch
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanya Wilson wrote:In Bank I am supposed to use a toString method, but I have absolutely no clue what it is or what it does.

Just to add to Daryl's excellent explanation, a bit more about what it does.

toString() is primarily a debugging aid. It's there to allow you to see what's in an instance of your class, not to create fancy reports or logging messages; so my advice: Keep it as simple as possible.

A lot of beginners go overboard with fancy formatting and tons of sophisticated labels. Don't. Just print out the contents in a way that's clear and easy to read.

Also: if your class contains other objects, use their toString() methods to display their contents. And if you're writing a subclass, use super.toString() to get the superclass contents. All this means that you should probably design the output to be in a form that you can easily add to, or include in a larger String, which is yet another good reason to keep it simple.

Thirdly: One method you might find useful when writing a toString() method is Arrays.toString(). You can use this to display the contents of pretty much any array or collection (because all collections have a toArray() method).

HIH

Winston
 
Tanya Wilson
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all very much. That helps me to understand the toString method quite a bit better. However, I'm still not clear if I should have just one toString method and call it in my other methods, or if I should have a toString method for all my methods. Hope that doesn't sound dumb haha.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanya Wilson wrote:or if I should have a toString method for all my methods. Hope that doesn't sound dumb haha.

Erm. Kinda.

You write methods (like toString()) for classes. You can't write a method for a method (well, I suppose you could, but it's not the sort of thing I'd advise).

Perhaps you could explain your confusion?

I should perhaps add that if the question is: "should [I] have a toString() method for all my classes?", the answer is: Yes.

Winston
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Winston says, yes, you should have a toString() method. But should you call it? Probably not. The method is mostly there so that other code can get a brief text description of an object. For example:



If you run this code, then something down inside the "println" method will call your walkIn object's toString() method and output the result to the console. There are plenty of other places where this sort of thing goes on but this is the standard reason for having a good toString() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!