• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how is toString method used in this question

 
Sneha Kapoor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


how is toString method used in this question

Source K&B book.Page 553
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sneha,

As toString method is method of object class... it`s always used to format the output of object.. when you say

System.out.println(obj);

obj.toString() method get called.. and the result of the method will be printed in out stream..


 
Prasad Kumbhare
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code example, toString() method has been overridden. So instead of printing object's name (it doesn't print object's name by the way), it will print custom message. In this example ,

DVDInfo d = new DVDInfo("Java","Ranch","Group");
System.out.print(d);

Output of this would be "Java Ranch Group"

Hope this helps.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic