FAQ: Learn Java: Methods - The toString() Method

#1

This community-built FAQ covers the “The toString() Method” exercise from the lesson “Learn Java: Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise The toString() Method

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like
#2

I don’t understand how System.out.println knows to connect with the toString method. What would we do if we had several classes that we wanted to print different messages about in main()?

#3

When we print out Objects, we often see a String that is not very helpful in determining what the Object represents. In the last lesson, we saw that when we printed our Store objects, we would see output like:

Store@6bc7c054

where Store is the name of the object and 6bc7c054 is its position in memory.

This doesn’t tell us anything about what the Store sells, the price, or the other instance fields we’ve defined. We can add a method to our classes that makes this printout more descriptive.

When we define a toString() method for a class, we can return a String that will print when we print the object

so the toString method is useful to see what an object represents, what you want would require defining methods to call on the object.

#4

Is toString a reserved word? What would have stopped me inadvertently making a toString() method to do something entirely different, only to find out that it was invoked when I tried to print the object?

#5

some things are the the developers responsibility, as developer you are mostly in control, so nothing would have stopped you

its an existing method yes, which you just overwrote for this object

toString is always invoked when printing an object.

#6

How does control passes to toString() method from main since we haven’t called it exclusively.

#7

its explained nicely here:

https://www.javatpoint.com/understanding-toString()-method

java calls toString automatically when you print an object.