Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Building the Model

question about the method toString()...

Why are we overriding the toString() method from the one that this included in the Java library? Why not create a method called "printInstance()" that does not require overriding?

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

toString() is a lot more general. You aren't doing a specific thing with it, like printing to the console. You could also use it to make label text for a GUI, for example. There are also situations where Java will automatically call toString() on an object to get its string representation. toString() is provided so it's available to all objects but it's also a method that's meant to be overridden.