File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to read this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to read this? " Watch "How to read this? " New topic

How to read this?

sal lazar

Joined: Jun 08, 2011
Posts: 1

Can anyone explain to me why the following code below returns 14 1?

vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81

hi ranch
count gets number 9 from the main method
and gets 5 from the maybeNew method

so sums to 14
and counter is one per object and you are accessing it from the last object its only 1

read carefully the code you ll get it ..... have nice time

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
vinayak jog, please read this:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
which is from the contents page for the "Beginning Java" forum. We usually delete such direct answers, but yours is quite vague, so I shall keep it here.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
sal lazar, welcome to the Ranch

Always tell us where the exercise is from; is that example from Head First Java™? Which page? Please be consistent with indentation and use spaces, not tabs, for indenting.

I think you will have to do it the hard way, with a pencil and paper., writing down the values of x, count and counter at each iteration through the loop. alternatively, yo ucan add a line to the loop which prints out those values, something like:If you are using m4a[x].counter, that line would have to go after your line 9. You can work out similar lines for other parts of the program. Then you can follow the flow of control and the values used. then yo ucan see where the 9 and 5 and 1 come from.
I agree. Here's the link:
subject: How to read this?
It's not a secret anymore!