• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confused with use of " this" in the constructor

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I came across a code in which the constructor of the class had this statement:
system.out.println(this);
Can somebody please tell me what this implies.It appeared on two mock exams.
Waiting eagerly to hear from somebody.
Thanks
Gazala.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
system.out.println(this);
will call the toString() method of that class. Unless toString has been overridden in this class or one of its superclasses(up the hierarchy) then the toString inherited from Object will be called which will return a memory location I believe.
 
Gazala Bohra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Randall.This is a interesting discovery.You have motivated me to run this piece of code and check out if it really prints a memory location, cause there doesn't seem to be a overridden toString method in this class definition.
Thanks again,
Gazala.
 
Gazala Bohra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Randall,
I tried that and you were right.I created a class 'Arbit' in a package 'test' and wrote this line:
system.out.println(this)
in the constructor of the class and here is what I got as a output:
test.Arbit@111f71
I hope this helps others too!
Thanks once again.
Gazala.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic