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.
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:
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 theirtoString() 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).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
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.
My honeysuckle is blooming this year! Now to fertilize this tiny ad: