• 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

Inner classes

 
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, need some help understanding How "Test" is written in output. That is, i dont know how is toString called.

Thanks in advance!

 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Mata wrote:Hi all, need some help understanding How "Test" is written in output. That is, i dont know how is toString called.

Thanks in advance!

interface TestA { String toString(); }
public class Teste {
public static void main(String[] args) {
System.out.println( new TestA() {
public String toString() {
return "teste";
}
});
}
}



Hi Joel,

I think the question is about toString() rather than inner classes.
You are implementing the interface TestA and overriding the toString() in an anonymous Inner Class inside printlnO() method
If you override public String toString() method, that will be printed when you print the class like new Test().
Please see the code below, i have taken your code and added couple more examples




hope this clears the question!

 
Joel Mata
Greenhorn
Posts: 25
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, yes the problem has the "toString".

So, the toString method is called when you issue a print on that object... Thanks
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Mata wrote:Hi, yes the problem has the "toString".

So, the toString method is called when you issue a print on that object... Thanks



you are welcome friend
and Happy preparation!
 
reply
    Bookmark Topic Watch Topic
  • New Topic