Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting different output in a Spring project

 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Could you please tell me why I'm getting a different output in the below Spring 3.0 program :-











This is my xml configuration file spring-idol.xml



I'm getting the below output :-

Playing DYNAMITE ...
using ...com.springinaction.springidol.Player@171bbc9
IN class Player, Playing VIOLIN.....


I was expecting "using VIOLIN......" in the second line, but don't know why it's different.
I cleaned up the project and rebuilt but no use..

Please advise.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because

System.out.println(getInstrument());

Is going to call toString on your Instrument, and toString default implementation in Object is to print the Object. Which in your instance of instrument at that moment is

com.springinaction.springidol.Player@171bbc9

Try overriding toString to print "violin"

Mark
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ... !!!
Thanks Mark.

 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try overriding toString to print "violin"


Hey Mark,

Since "VIOLIN.." is defined in the conf file and which might change in future, what can I write during the @Override of toString() ?
or can I avoid this @Override and still get 'VIOLIN' printed using Spring ?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you didn't post all the code. How does "using…" printout. Where is that code?

The only other way besides overriding toString is to cast your Instrument to the actual type to get to the String instrument variable value. But that would remove you from coding to an interface which is a best practice to always code to an interface. So for me the only option you have and what should be done is overriding toString. Look at Object classes implementation of toString and you will see why you got your value.

Mark
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mark,

I just added System.out.println("using " + getInstrument()); to the original System.out.println(getInstrument()); to make it more meaningful. Sorry for missing it out over here..

The only other way besides overriding toString is to cast your Instrument to the actual type to get to the String instrument variable value.


You mean to say just like the other variable song which I written?
I thought so and that's why made it bit different to see what would happen..
Besides, I also changed few things like adding setters/getter and and trying out different things in xml file , but couldn't overcome that problem.
Will try out the overriding method and let you know.

Thanks a lot.


 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main point I am saying here, at this point. Is that your issue is a plain Java issue, that you would have using these classes without Spring. Spring isn't causing the Instrument's toString method printing the object reference.

Mark
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic