• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

New Objects keeping old values

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a school assignment and I have run into an issue where I am creating 4 objects and in each object I have to get a total price of all the items in the order. So I create 4 order objects and I fill it with information, one of the items is "final price" and for the first object everything is fine the total keeps going up as needed, but when the loop is done and a new object is created the new object keeps the "final price" value and does not reset back to zero. I thought when a new object was created it would go back to zero and start a new final price value. Can anyone take a look at this file and see if I am doing something wrong (which I am sure I am)



If you want the other files to do this here they are:



 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you could point to a line of code or an output that shows your error. Otherwise, to help you, I first have to figure out what you mean by "when the loop is done" and where you're creating this object and what evidence you have that the amount is incorrect.

rc
 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ralph Cook wrote:It would help if you could point to a line of code or an output that shows your error. Otherwise, to help you, I first have to figure out what you mean by "when the loop is done" and where you're creating this object and what evidence you have that the amount is incorrect.

rc



I have updated the file with some comments on what is going on in the GadetOrderTake file, and below is the output when I try to do the orders...

12.99Quantity is: 1

12.99
7.55Quantity is: 1

20.54
9.99Quantity is: 1

30.53
6.89Quantity is: 1

37.42

Above is the first object and I just have system outs showing the price of the item orders, the quantity and the total price at that time, as you can see I am entering just 1 of the item and the total price is going up fine, but below is where the new object is created (so it asks for a new customer name and address) and then the total price just keeps going up where I would expect instead of 47.41 I would see 9.99

9.99Quantity is: 1

47.410000000000004
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John-Paul,

At the top of your class you've got a bunch of working variables, some of which are declared as static. You might want to think about why you've made some of them class (static) variables, and some instance variables, and what the difference is between those.

(You might also ask yourself which of those variables should really be declared at the class level and which should be local to the main method.)
 
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you got so many static fields? You may not understand what static means, and a static field might retain its value between successive objects.
 
Campbell Ritchie
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and why have you given the fields protected access rather than private?
 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to say thanks guys...I went back and reviewed static variables and from what I understand now is that because they where static it was only making one variable and all the objects I was making where accessing the one variable. I say this only because maybe I am still wrong, but once I read that I cleaned up the top of my file and moved the variables to a different spot and it now works

 
Campbell Ritchie
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you make a field static, all instances have access to it, and that field may store old values.
reply
    Bookmark Topic Watch Topic
  • New Topic