Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

toString method is not getting invoked for the another class

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have 2 classes, Quote and Value. In Quote class, I have an variable for type Value. In both classes , I have implemented toString() method.
Now I am calling the toString() method using below code.



I was expecting the values in both Quote and Value classes readable. But the toString() method from Value class is not getting invoked. The code is below. Could you please help why it is not invoking.




 
Ranch Hand
Posts: 121
12
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Rajesh

This is a very good question.

The type of the value variable is not what you expect! It is not your webservice.Value. It is com.fasterxml.jackson.annotation.JsonFormat.Value. That's why toString in the webservice.Value is never called - these values have never been part of the Quote.
Imported type always shadows types in the same package, see JLS, 6.4.1. Relevant section:


A single-static-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:
* ...
* any top level type (§7.6) named n declared in another compilation unit (§7.3) of p;



In your example solution is simple: just remove an offending import. If it is not possible (both classes are needed), you have to use fully-qualified names (i.e. full type name with its package) for all but one types with the same simple name (and you could import only that required one).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic