• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Battling with code example: JSON/gson

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi - I am using eclipse to try an example located at: how to convert JSON strings to JAva object

the code is:


When I run it I get the following n the terminal:
<terminated> App [Java Application] (the path)
myproject2.UserDetails@41cf53f9
I also get warnings that none of the fields in the User details are being used
Screenshot enclosed.
Any advice would be great.
Thanks
Screenshot_1.jpg
[Thumbnail for Screenshot_1.jpg]
 
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Russell Sher wrote:When I run it I get the following n the terminal:
<terminated> App [Java Application] (the path)
myproject2.UserDetails@41cf53f9


Try adding a toString() method to your UserDetails class which returns a String containing the values of the instance variables.  This will override the default implementation from the Object class of class-name@hash-code, which is what you are seeing.


Russell Sher wrote:I also get warnings that none of the fields in the User details are being used


That is because you have private variables which are not referenced anywhere inside the class.  Normally you would have getter methods so that you could access the values of the variables from outside the class.
 
Sheriff
Posts: 17345
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron has already explained the reasons but the output you see is correct, given the code that you wrote. What were you expecting it to display?

The myproject2.UserDetails@41cf53f9 displayed is what you would expect from line 19 in your code listing because the UserDetails class does not override the toString() method inherited from the Object class. What you're seeing there is what the inherited Object.toString() returns.
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and thanks for the replies,
What I was expecting was as described on that web page of the example where it shows the following as in the screen capture:

I'm just battling to work out how to add the toString() method in the Userdetails class
I'm trying to add a function to do this inside class Userdetails, can you perhaps guide me a bit more- I tried the following






thanks
Russell
Screenshot_2.jpg
[Thumbnail for Screenshot_2.jpg]
 
Junilu Lacar
Sheriff
Posts: 17345
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't do that. System.out.println() will call an object's toString() method so what you want to do is override toString() in your UserDetails class.

Search for "how to override toString() in Java"
 
Junilu Lacar
Sheriff
Posts: 17345
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Debugging is certainly one way you can view the field values of an object. In my opinion, the debugging tool in your IDE should be your last resort. Some of the best programmers I know hardly ever use their debuggers. Instead, they write automated unit tests that relate what exactly they expect their programs to do.

For example, here's a test I might write to verify some of the code you wrote:

Of course, this test wouldn't work with the code you wrote but if I moved some things around and added appropriate methods like getName() and getAge() to the UserDetails class, it would.

I think it's unfortunate that many new programmers focus on learning tools and frameworks like this without the benefit of learning how to properly test and verify their code. Unit testing is one of the most important skills you should be developing as a programmer. As Kent Beck, the author of "Extreme Programming Explained: Embrace Change" and original signer of the Manifesto for Agile Software Development said, "If you can't write a test for it, you have no business coding it."
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok - I did this (after some reading that you suggested), which seems to work - I'm just showing the name attribute for now.
Is the following 'ok'practice ?
(Just showing the UserDetails  class)
 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an incorrect usage of String.format(). This method usually is given a minimum of 2 arguments, the first of which is a format string. You've only given it a format string. If 'name' contains none of the special formatting characters you will get away with this but it is very risky. A better way would have been to call format("%s",name). This will always work correctly but at this point it also adds no formatting, so just returning 'name' is cheaper and more succinct. If you are going to eventually add more arguments then using format() is appropriate but then add a default format string.
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - right you are . Have changed it.
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So one question arises from what I have learned above;
by executing the code:

I see it automatically calls the method toString() which is in the UserDetails class
I also can execute this method of course, by calling: from main.
So, is it correct to say that: calls and executes an instance of the class and any method in it? And is this another way that one might execute methods, by calling an instance of the class ?
Sorry, still learning here.
Russell
 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Russell Sher wrote:. . . it automatically calls the method toString() which is in the UserDetails class . . .

Since Object declares the toString() method, every class ever written has that method.
If you look up System#out, and follow the links therein, you will eventually get to this, which tells you it uses String#valueOf(Object). If you look up String#valueOf(), you find it tests whether its argument is null, otherwise it calls toString() on it. If you write this sort of thing:-...youi will find it is better to leave the call to toString() to the ready‑made methods which have been designed to cover all possibilities.
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply - I'll check those links
Russell
 
Junilu Lacar
Sheriff
Posts: 17345
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Russell Sher wrote:So, is it correct to say that: calls and executes an instance of the class and any method in it? And is this another way that one might execute methods, by calling an instance of the class ?


In the programming world, we don't usually say we call/execute a class or object. Classes and objects are things. Methods are capabilities or behaviors. Make sure you understand the distinction.

When we create a new object of a class, we say we "instantiate that class" or "we create a new object" of that class.

Methods, on the other hand, are capabilities or behaviors. You can think of methods as "superpowers" of a class/object. If we want a class/object to do something, we "invoke/call/execute" a particular method of that class/object. This usually causes something to happen as a result of performing the capability or behavior that is coded in the method.

In old-school object-oriented parlance, invoking or calling a method was referred to as "sending a message" to the object. That term is not used very often these days but it's still good to know that it means the same thing as "invoking/calling/executing" a method, just in case you come across that term in your studies.

Sorry, still learning here.


Around here, there's no need to apologize for being a learner.
 
Bartender
Posts: 1899
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below prints the name.

I also made your class private and static so you could call it from your static main method.

------



Prints:

Inside Userdetails...
Name is: John
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, many thanks for the help
Regards
Russell
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic