GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Writing own toString() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Writing own toString() method" Watch "Writing own toString() method" New topic
Author

Writing own toString() method

Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Which is the best way to write our own toString() method?


public String toString()
{
String str = "My name is " + name + "\n"; // name is an instance variable
return str;
}


OR


public String toString()
{
String str = "My name is " + this.getName() + "\n";
return str;
}


Obvoiusly the first one will definitely work even when the getName() method is not declared.
But second one can only work if the getName() method is declared.

But it is always better to use accessors to get the value.
Which one to follow - what is the Java convention?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Use your best judgement. Java norms doesnt dictate anything on this. You may even use "this.name". But creating a method called getName() for returning just a string, that too to the same class, without doing any operations in specific to the input string doesnt really make sense.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
All those options would work, but I suggest you leave out the \n. Then users can use System.out.print and System.out.println and get different results. Also \n can (rarely) fail to work because of platform differences. I would actually write something like
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I agree with Campbell that you don't need the /n here. However, If your to String is very long, or your object has a lot of variable, it can be useful for formatting purposes. Not at the end of the string, but at various points within the string.

I often use toString() method for debugging and development. I will stick toString() calls into my code at certain points just to help figure out what my instance variables are at a given time, from there I can determine why things are happening. Once the problem is fixed, the toString() call is removed, but the actual toString() method stays with the class, in case I need it in the future
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
I would suggest if your String description is very long, it is better to use the %n tag and the String#format method. Or shorten the String??

You don't usually say "toString" when you print.That calls the toString method automatically.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:I would suggest if your String description is very long, it is better to use the %n tag and the String#format method. Or shorten the String??

You don't usually say "toString" when you print.That calls the toString method automatically.


hmm didn't know that about just specifying the object reference as a println argument, I'll use that. Thanks.

It's not clear to me why there is anything intrinsincally (sp?) wrong with long strings or using the "\n" parameter within the String. I would think it depends on the situation. It's certainly easy to do. And it works fine for me. I'll look into %n and String#format, and maybe I'll see a reason, but it sounds a little like extra work to accomplish a very simple task.

regards.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Chikito Charles wrote:Obvoiusly the first one will definitely work even when the getName() method is not declared.
But second one can only work if the getName() method is declared.

But it is always better to use accessors to get the value.
Which one to follow - what is the Java convention?

People have been going off-topic and discussing other aspects of what you posted, but let me point out something about this.

Suppose you have a getName() method which simply returns the name variable. Then using "name" and "getName()" in your toString() method would appear to do the same thing. In fact for that class, they DO do the same thing.

But now suppose you write a subclass of this class that you were talking about. Suppose that it overrides the getName() method to do something completely different. And suppose that it doesn't bother to override toString(). Now what happens if you have an instance of the subclass, and you call its toString() method?

Right... the toString() method calls the overridden getName() method of the subclass, so what it returns isn't the "name" variable, it's the completely different thing returned by the subclass's getName() method.

Now maybe this is a bad thing. Or maybe it isn't. Personally I don't think it's a bad thing. From a design point of view, if you're going to encapsulate your "name" variable by providing a getName() method, you should really use getName() everywhere and not take shortcuts internally. If you allow toString() to use "name" directly, you are subverting that encapsulation.

The other reason why it isn't a bad thing is that the person writing the subclass is free to write their own toString() method if they don't like yours. And they should, anyway.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
The more recent versions of the Java™ Tutorials section about formatted printing recommend always to use %n instead of \n. It gets the line-separator which the operating system expects. 99% of the time, operating systems can cope with \n even when they prefer something else, but there is always the odd chance that something peculiar will happen.

The only thing wrong with a very long String is that it may become difficult to read.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:The more recent versions of the Java™ Tutorials section about formatted printing recommend always to use %n instead of \n. It gets the line-separator which the operating system expects. 99% of the time, operating systems can cope with \n even when they prefer something else, but there is always the odd chance that something peculiar will happen.

The only thing wrong with a very long String is that it may become difficult to read.


Indeed, which is why we use #format & %n. or \n

ok, your explanation of "\n" makes perfect sense now. It simply might not work on some operating systems. Definitely worth considering if I distribute my software. Do you know if that o/s compatibility issue applies to writing strings to a JTextArea in a JApplet that is on the Web?

Thanks for taking the time to explain. I hope it's ok to discuss side issues like this in these threads, if not let me know. I usually learn something from the discussion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
We usually end up discussing side issues anyway; as you say, you pick up interesting information like that. The only application I can think of offhand which has problems is NotePad; it is liable to show \n as a little box and not start a new line. Probably best to use all portability tricks however.

But Paul is right that we ought to answer the original question.
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Thanks to all especially to Paul and Dawn - I understood better now.
 
GeeCON Prague 2014
 
subject: Writing own toString() method