Omitting the Method toString

In this lesson, we look at a class that does not define its own toString method.

The class Name without the toString method

We have previously mentioned the importance of a class having its own version of the method toString. Let’s see what happens when we do not define toString. For example, let’s remove the definition of the method toString from the class Name, as defined in the previous lesson, and name the revised class Name3. Let’s also retain the client, DemoName, but change its main method to the following one:

public static void main(String[] args)
{
   Name3 joyce = new Name3("Joyce", "Jones");
   System.out.println("Joyce's name is " + joyce);
} // End main

Click the RUN button to see what happens. Do you expect to get an error message?

Get hands-on with 1400+ tech skills courses.