How to correctly override toString () in Java?

Sounds a little silly, but I need help with my toString () method, and it is very annoying. I tried to find it online because toString is the one where it spins and “does not find Kid constructor No. 2”, although it exists, and I would even do something else, and it does not work. Ok, that was it, here is my code:

import java.util.*; class Kid { String name; double height; GregorianCalendar bDay; public Kid () { this.name = "HEAD"; this.height = 1; this.bDay = new GregorianCalendar(1111,1,1); } public Kid (String n, double h, String date) { // method that toString() can't find somehow StringTokenizer st = new StringTokenizer(date, "/", true); n = this.name; h = this.height; } public String toString() { return Kid(this.name, this.height, this.bDay); } } //end class 

Okay so, my toString above (I know my third parameter is off, should be String) is off. If I rigidly set the value for the third thing, he goes into haywire and says that he cannot find it (at the top). So how can I get the date and break it down?

The class calling it below

 class Driver { public static void main (String[] args) { Kid kid1 = new Kid("Lexie", 2.6, "11/5/2009"); System.out.println(kid1.toString()); } //end main method } //end class 

I tried to research several constructors and it really didn't help. I tried to learn the toString () methods and tried to use the previous logic of the toString () methods that I created earlier, but this is completely new, so it never worked.

Help?

+78
java
May 24 '12 at 8:49
source share
7 answers

It is assumed that toString returns a String .

 public String toString() { return "Name: '" + this.name + "', Height: '" + this.height + "', Birthday: '" + this.bDay + "'"; } 

I suggest you use your IDE functions to generate the toString method. Do not specify it manually.

For example, Eclipse can do this if you just right-click the source code and select Source > Generate toString

+126
May 24 '12 at 8:52
source share

Java toString () Method

If you want to represent any object as a string, the toString () method appears.

The toString () method returns a string representation of the object.

If you print any object, the java compiler internally calls the toString () method of the object. Thus, overriding the toString () method returns the desired result, it may be the state of the object, etc. Depends on your implementation.

Advantage of the Java toString () Method

Overriding the toString () method of the Object class, we can return the values ​​of the object, so we do not need to write a lot of code.

Exit without toString () method

 class Student{ int id; String name; String address; Student(int id, String name, String address){ this.id=id; this.name=name; this.address=address; } public static void main(String args[]){ Student s1=new Student(100,"Joe","success"); Student s2=new Student(50,"Jeff","fail"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } Output:Student@2kaa9dc Student@4bbc148 

You can see in the example above # 1. printing s1 and s2 prints the Hashcode values ​​for the objects, but I want to print the values ​​of these objects. Because the java compiler internally calls the toString () method, overriding this method returns the specified values. Let me understand this with the example below:

 Example#2 Output with overriding toString() method class Student{ int id; String name; String address; Student(int id, String name, String address){ this.id=id; this.name=name; this.address=address; } //overriding the toString() method public String toString(){ return id+" "+name+" "+address; } public static void main(String args[]){ Student s1=new Student(100,"Joe","success"); Student s2=new Student(50,"Jeff","fail"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } Output:100 Joe success 50 Jeff fail 

Note that toString () is mainly related to the concept of polymorphism in Java. In Eclipse, try clicking on the toString () button and right-clicking on it. Then click “Open Declaration” and see where Superclass toString () came from.

+10
Jul 02 '15 at 1:31 on
source share

You can create a new object in toString (). use

 return "Name = " + this.name +" height= " + this.height; 

instead

 return Kid(this.name, this.height, this.bDay); 

You can change the returned string if necessary. There are other ways to store dates instead of calendars.

+4
May 24 '12 at 8:53
source share

You cannot call the constructor as if it were a regular method, you can only call it with new to create a new object:

 Kid newKid = new Kid(this.name, this.height, this.bDay); 

But creating a new object from your toString () method is not what you want to do.

+3
May 24 '12 at 8:53
source share

The following code is a sample. A question based on the same, instead of using an IDE-based transform, is there a faster way to implement so that changes occur in the future, we don’t need to change values ​​again and again?

 @Override public String toString() { return "ContractDTO{" + "contractId='" + contractId + '\'' + ", contractTemplateId='" + contractTemplateId + '\'' + '}'; } 
+2
Sep 09 '14 at 13:23
source share

Well, actually you will need to return something like this, because toString should return a string

 public String toString() { return "Name :" + this.name + "whatever :" + this.whatever + ""; } 

and you are actually doing something wrong in the constructor, which you set to the variable that the user set for the name, while you need to do the opposite. What you should not do

 n = this.name 

What should you do

 this.name = n 

Hope this helps thanks

+1
Jul 02 '15 at 2:51 on
source share

we can even write like this by creating a new String object in the class and assigning it what we ever want in the constructor, and return it to the toString method, which is overridden

 public class Student{ int id; String name; String address; String details; Student(int id, String name, String address){ this.id=id; this.name=name; this.address=address; this.details=id+" "+name+" "+address; } //overriding the toString() method public String toString(){ return details; } public static void main(String args[]){ Student s1=new Student(100,"Joe","success"); Student s2=new Student(50,"Jeff","fail"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } } 
+1
Dec 20 '16 at 15:53
source share



All Articles