How to Use the toString Method in Java

Java


In this article, we show how to use the toString Method in Java.

The toString method in Java is a method that allows you to represent an object as a string, so that you can visually examine the object in String form.

The toString method returns a string representation of an object.

The toString() method is invoked when you use the System.out.println function specifying the object name in the parameter of the System.out.println function.

The toString() method is especially useful when you are dealing with encapsulated code, private properties and getter and setter methods.

With encapsulated data, you would have to use a bunch of getter methods to get all the data of an object.

With the toString() method, you can get all the data of an object in String with this single method.

So it's worth learning.



So we create a class named Students.

In this class, we declare 2 variables, a String named name and a private int named age.

These variables represent the name of age of student objects, which we will declare later on.

We then create a constructor with 2 parameters, a String named name and an int named age. We assign the object being instantiated with the values passed in representing the name and age of the student.

We then create our 2 setter methods, setName and setAge. These methods set the name and age of the students.

We then create the getter methods, getName and getAge. These methods get the name and age of the students.

We then come to our toString() method.

So now we get to the heart of it.

So a toString() method in Java always has a return type. In this example, it's a string. A toString() method, as the name implies, will almost always have a return type of String. It's a string representation of an object. So you can see above how the declared return type is a String, public String toString().

So now that we've gotten the declaration out of the way, we go to the body of the toString() method. In the body, being that we declared the return type of String in the declaration, it must have the return keyword in the body. It's not a void method. Only a void method in Java doesn't need to use the return keyword. So being that we declared the toString() method to be of a String return type, it must return a String. So in the body we write the return keyword and we follow it with the object parameters we want to specify. Usually this is all the properties that make of an object, so that we can visually see the object as a string.

So in this code we create a class named students. Each student object has a name and age property. So in the string, we put this age and name property. We use the keyword this to refer to the current object which has been called and we use dot followed by the property name to access the property. So in the exampel above, I put the student name is how many years old. That covers all the object properties.

As you'll see, when I show the test class to run this code and this toString() method, this method gets invoked when you use the System.out.println() function and put the object name as a parameter. That's when the toString() method is invoked.

Java Test Class

To test out the code above, we create a separate file and put the following code below in it.



So in this create a file named studentsdemo.

We put our main function in this code.

We create a student object named student1 and pass the parameters "Michelle" and 7 representing the name and age of the student. This initializes this object.

We then create a System.out.println() method with the object name, student1, specified as the parameter to this method.

Putting the object name directly in the console output function invokes the toString() method.

So whatever is in the toString() method gets output, anything after the return keyword.

So, as output, we get the following below.

Java Output

Michelle is 7 years old


So, as a recap, the toString() method allows us to see the visual String representation of an object.

Without the toString() method, we would have to use a bunch of getter methods to obtain the internal data for an object. With the toString() method, we can just simply call and the object and output all the object properties. We don't have to use a bunch of getter methods. We can just use one single method to see all the object properties. So it simplifies getting output of an object greatly. For this reason, it's used a lot of for troubleshooting.


HTML Comment Box is loading comments...