0

I have an object and I can do:

System.out.println(o);

Which is the "hidden" method called to get the object description ?

Always toString() or does it depend from the object type ?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Massimo
  • 3,171
  • 3
  • 28
  • 41
  • 2
    In this case, it's always `toString` – MadProgrammer May 03 '17 at 23:07
  • It's not "hidden". It's declared in the `Object` class. you can override this method in any class you'd like to have a custom print String. – Jake Miller May 03 '17 at 23:09
  • well if you override the `toString()` the `string` representation of that object will be called otherwise the `toString()` of the `Object` will be invoked. – Ousmane D. May 03 '17 at 23:09
  • 2
    Possible duplicate of [Why is the toString() method being called when I print an object?](http://stackoverflow.com/questions/8555771/why-is-the-tostring-method-being-called-when-i-print-an-object) – fujy May 03 '17 at 23:10
  • You can check this answer for an how-to implement/override the toString() method for your own needs : http://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java – Nebu May 03 '17 at 23:32

0 Answers0