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

passing on values from method to method

Christopher Blasnik
Greenhorn

Joined: Oct 31, 2001
Posts: 4
Hello all,
i have noticed some odd behaviour from methods:
if u have a class with 2 setMethods for example, and one passes its calculated value on to the other method, the return value i get from the latter will always be the same as when the corresponding instance variable was initialised, to make myself more clear, i did something like this:

so as you can see i tried to pass on a parameter from one method to another one, within the same class
but now comes the odd thing (at least odd to me for i dont understand it :-) :
when constructing an object in your main method that accesses the above class the following will happen:
if i call for example (assuming i have constructed an object variable named "testicus"):
System.out.println(testicus.getY());
this will print the correct value (whatever number was entered)
BUT:
int number = testicus.getY();
will assign a value of 0 to "number"! (the value which was used to initialize the "y" instance variable in the class Test
basically this does NOT happen if i make a direct call from my main method like this for example:
testicus.setY(5);
if i try
int number = testicus.getY();
again, it will assign the correct input value to the var "number".
So my questions are: what happens to values when they are passed on from one method to another one?
Why can't they be assigned correctly to a variable in the main method (instead giving them their initialisation values)?
Is there any way how i still can get my desired outcome (assigning a variable in the main method with the correct result) using methods to pass on parameters to themselves?
i have been looking all over the internet and couldnt find an answer. i would really appreciate your help!
thanks in advance,
chris
[This message has been edited by Cindy Glass (edited October 31, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Could you show us your main method that you used to test this?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Chris
The following code works fine for me:

The output I get is:
x: 0 y: 0
Setting x and y to 7
x: 7 y: 7
number is: 7
Did I do it differently than you did? The other question would be to ask when did you try to set and read the values of X and Y? was it in a constructor? If so then the object may not have been completely instantiated yet so the values of the instwance variables would be 0.
Can you post an example of the complete code that gives you the output you talk about?


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Christopher Blasnik
Greenhorn

Joined: Oct 31, 2001
Posts: 4
yes suddenly it is working for me too?!?
but i will dig out the code that is the actual problem (it is a first year CS task i was assigned)
just give me 30 mins )
thanks a lot, chris
Christopher Blasnik
Greenhorn

Joined: Oct 31, 2001
Posts: 4
ok here comes the code..
as i have stated in my last posting, this is a task for my CS course: a user has to type in every detail of a book (title, author, numberinstock, priceperbook, number he wants to order) and then the price should be calculated.
the code for the class Book:

as you can see, the last two lines SHOULD have the same output, but they dont:
x puts out 0.0
while newOrder.getCalculatePrice() puts out the correct value!
this is very odd IMO and i just cannot find another solution but to make the method setCalculatePrice public and enter the parameters from the main method instead from within the class Book
btw: i hope i haven't made any mistakes pasting the code, but pasting from the vi editor to some browserwindow w/o using a mouse is a pain in the butt :-)
chris
(edited by Dave to format code)
[This message has been edited by Dave Vick (edited October 31, 2001).]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Chris
In the code you posted you set the orderPrice to 0 when you declare it. The only time you change it is in the setCalculatePrice method, and the only time you call the setCalculatePrice method is in the getNumberInStock method. In you example you set x to equal the return value of getCalculatePrice() but when you do that you haven't called the getNumberInStock method so the orderPrice is still 0. Then a few lines later when print the numer of books ordered you call the getNumberInStock method which in turn calls the setCalculatePrice method which sets the orderPrice variable so that when you you print it out at the end it is correct.
To test it, move your line 'double x = newOrder.getCalculatePrice();' to just before the println statemnet where you print it out and both numbers will be the same.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
Christopher Blasnik
Greenhorn

Joined: Oct 31, 2001
Posts: 4
First of, thank you for the quick answers Dave + Cindy
1.
the reason why i assigned 0 to my numbers and an empty string to my Strings is because i read somewhere that variables should always be intitialised, no matter what value you give them
What do you think about that?
2.
now everything is clear...IMO the most convenient (or rather the "best looking") solution would be to move the calculating part from getNumberInStock to setNumberInStock and it should have the same effect as moving the line in the main method?
now it's obvious why it didn't work! ...i am still in the process of getting used to programming - seeing things in a greater context -, cause so far all i have done is a bit of pearl hacking...two totally different worlds :-)
thanks,
chris
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Chris
As to setting your variables to default values it is never a bad idea to have a definate starting point, then you always know where you stand with them. On the other hand they will be automatically set to default values (0 for numeric and null for reference) when an instance is created.
On your second point, from a functionality stand point it might make better sense to just call your setCalculate price manually. For instanc what if down the line you modify the program to let people change the price of the book, or the cost of shipping, if either of those changes the total price should be recalculated, but only if there are values in the other fileds that make up the total price. You can either test for values in the setCalculatePrice method and throw an exception if one is missing to present a message to the user. Or you could just call it when you know that all of the variables have values.
On the other hand if this is an assignment then you might have been told where to put it, in which case - ignore everything I just said
As for myself (and probably Cindy too) we're glad to help!!

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
 
subject: passing on values from method to method