aspose file tools*
The moose likes Beginning Java and the fly likes New Objects keeping old values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New Objects keeping old values" Watch "New Objects keeping old values" New topic
Author

New Objects keeping old values

john-paul York
Greenhorn

Joined: Feb 15, 2011
Posts: 16
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:



Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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

Joined: Feb 15, 2011
Posts: 16
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
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
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.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
. . . and why have you given the fields protected access rather than private?
john-paul York
Greenhorn

Joined: Feb 15, 2011
Posts: 16
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Yes, if you make a field static, all instances have access to it, and that field may store old values.
 
jQuery in Action, 2nd edition
 
subject: New Objects keeping old values