• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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).
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic