• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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).
 
Let me tell you a story about a man named Jed. He made this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic