• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I print out the values of instance variables of an object?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following code:



As you can see, I have created two objects. One object took weight and speed as arguments, and my second object took only speed as an argument.
Now, what is my proof that obj1 indeed holds 5 and 6 as the weight and speed values? same for obj2. How can I have these objects print out in a manner so that they reveal their states?
I'm assuming that I would need to implement the toString() method, but I'm not sure how...
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
implement the toString() method,

Yes, that is a common way.  Define the toString method as defined in the Object class and have it build the String with the values you want to see printed and return it.
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
implement the toString() method,

Yes, that is a common way.  Define the toString method as defined in the Object class and have it build the String with the values you want to see printed and return it.


I just know that I have to use the toString method, but I don't have the slightest idea as to how it would look in code

If you can please show me. Thank you.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
There are 3 things needed:
1) define a method that returns a String with the name toString
2) build a String:  Use concatentation operator: String retval = part1 + value1 + part3 ...etc
3) return the String: return reval
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you in constructor define the data, later your method toString() can look like:


When you will print your objects like:


The results can look like:
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define the data into constructors like:

  
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Vasyl Lyashkevych  Try not to spoonfeed too much code to the OP.  Do let him do some thinking and less copy and pasting.
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, understood
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:@Vasyl Lyashkevych  Try not to spoonfeed too much code to the OP.  Do let him do some thinking and less copy and pasting.


I had a teacher once who would make us put away our pencils as he did not believe in note-taking. While this approach may have worked wonderfully for him, I needed to take notes, make drawings, review the material in order to grasp it. Professionals in their fields often become tunnel-visioned in a way where they feel that the way that they got to a certain point is the only way to get to it. That being said, I respectfully insist that the answer is "spoon-fed" to me as it allows me to grasp the material much better. Once I am given the answer, I create various versions of it and play with it to fully understand it. Otherwise, I spend an entire day trying to grasp a concept which is fairly basic and I don't move forward, which is demoralizing. This is not homework for me - this is time I am taking out as an unemployed person to pick up a skill. Thank you for understanding

tl;dr: we all learn differently.
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will be more attentive in the future
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasyl Lyashkevych wrote:I will be more attentive in the future



So far, I have this but it has errors. What am I doing incorrectly?

 
Junilu Lacar
Sheriff
Posts: 10950
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toString() method on line 17 does not take any arguments and yet you call it with an argument on lines 26, 27, and 32.
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't write like:


but like:

       
Because in the method :


you need to write how do you present you object into the string for output
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The toString() method on line 17 does not take any arguments and yet you call it with an argument on lines 26, 27, and 32.


Okay, I fixed this and my code is as follows:




However, my output is as follows:



I was expecting the values I had passed into the constructor... what happened?
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you return "null" like here:



why did you return null? In this case, you don't need this method.

 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you return something, that something will be written like:

 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasyl Lyashkevych wrote:You don't write like:


but like:

       
Because in the method :


you need to write how do you present you object into the string for output




The output I am getting is this:



The code is as follows, currently as per your suggestion:



 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My results look like:
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is true, because you wrote:



You need to replace these two methods to single:
   

At all!

 
Paweł Baczyński
Bartender
Posts: 2047
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you don't need to write

You can write
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are your member variables static?

Don't make them static unless you understand what that means and you're doing it for a specific purpose (and not just to make errors that you don't understand go away).

By making the member variables static, you've made it so that there is only one copy of these variables which is shared by all objects of type OverloadedConstructor. That is normally not what you want. You want each OverloadedConstructor object to have its own copy of the member variables, so you should not make them static.

This page from Oracle's Java Tutorials explains more about static: Understanding Class Members
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasyl Lyashkevych wrote:You don't write like:


but like:

        . . .
No, you do it differently. Start by going to System.out.println in the API documentation and you will find it difficult to find. Finding System is easy enough, but you won't find println there. But if you look in the System#fields link, you will find something called out and it says there it is of type PrintStream. That means the System class has a static field called out, described as the “standard” output stream. If you click the out link, you get more details with a little example of usage, but that is all it says in System. So go to one of the see also links about PrintStreams: try the one taking Object as its argument. When you open that link, it tells you about String.valueOf and various other print calls. The bit we are interested in is String.valueOf() but no link is provided, so you have to go to the String class to look for it: try this link. There is tells you that method returns “null” for nulls, otherwise it calls toString.

We have now found out that System.out.println(myObject) automatically calls toString. There is therefore no need to write toString in println calls. You therefore simply write:-You can pass absolutely anything to System.out.println as long as you have not more than one argument, and the compiler will find some version of the method that can handle it. It will print something, even though this version may appear very laconic.
 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You always want to override Object's toString() method, and a good way to make sure you're doing this is to use the annotation @Override:
 
Vasyl Lyashkevych
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Sheriff. Thanks, It was useful for me too!
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Knute said: a long time ago there were lots of problems because people were spellling thinges wrongley (e.g. hashcode tostring rather than this or this) or changing the parameters, which means that rather than overriding a method one was creating a new method. You spelt toString correctly and created a new method by giving it parameters. In Java5 (2004) the annotated types were introduced, including three predefined annotated types (there are more nowadays). The @Override annotation is intended for the use of the compiler; it means, “only compile this method if it really overrides a superclass method.” The exact meaning of that annotation changed in Java6 (2006) to include interface methods.
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I had previously mentioned, I don't learn well by being given bits and pieces. I am still essentially at the same place.
Now, I have two classes: One is the launcher class which contains the main() method,  and the other contains my code. I have been unsuccessful in implementing the toString() method.
Also, the console output I am getting is 0 and 0 obj1.weight and obj1.speed, respectively.

My Launcher Class:



My Code:



Console Output:



I was expecting 5 and 6 as the console output. Why am I getting 0s?
 
Mark Richardson
Ranch Hand
Posts: 102
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed. Although, I'm surprised that I didn't have to utilize the toString method.. how come?

 
Knute Snortum
Sheriff
Posts: 3974
98
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your goal was just to print two fields of an object, then you've done all you need to.  If you want to be able to print an object, not just some of its fields, then you need to override the toString() method.
I don't learn well by being given bits and pieces.

Unfortunately, in the Beginning Java forum, that's how we normally work, but for now, here is a concrete example:

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!