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 question about variables changed inside methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about variables changed inside methods" Watch "question about variables changed inside methods" New topic

question about variables changed inside methods

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
Does this work?

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
What happens when you try to compile and run it?

There is no emoticon for what I am feeling!
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
Well, the problem is the same one in my thread "problem with StringBuffer and Double," which is a bit more complex, but I am thinking that this is the kind of problem. The code in that thread compiles and runs, but it doesn't print the individual employees' gross and net amount, though it does print the exactl total for all employees. I just thought that my code (above) might be simpler to try and work with.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
hello ,

No it will not work , you will get compile time error

non-static variable cannot be referenced from a static context

make variable x, y , as static

No no-static variable or function can be accessed from a static context

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Indeed, the variables need to be static, but I think you know that. This program will act in a potentially surprising way because although methodOne() assigns a value to the parameter "z", the member variable "z" remains unchanged. The parameter "z" and the member variable "z" are completely unrelated, and perhaps that's the root of the problems you're having.

There are several practices here that are bad habits, and perhaps I should just point them out. One is naming local variables or parameters the same as member variables. Although this isn't technically wrong, it's always confusing and there's rarely an excuse for doing it. In this program I suspect that fact that you're reusing names is confusing you.

Another thing that's generally bad (although this one is more subtle) is writing methods which are useful only for their side effects. If you have a choice between writing


and then calling it as "c = method(1, 2)", the second choice is generally preferred. There are many reasons for this, but the two most important are that it makes it more obvious what the method does, and that it makes the method easier to test in isolation. Methods whose explicit purpose is to set a variable are OK, by the way. That's why I said this point is subtle; it's a judgement call.

[Jess in Action][AskingGoodQuestions]
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
I'm getting "cannot find symbol" in processPay() and printEmployeeInfo()

I am lost in this code...
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
here is my latest idea. the method processPay() internally sets the value of the two Doubles in question. what's wrong with the way I'm doing this? I'm so befuddled I don't know what else to ask/try/say...

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
It still won't work like you think it does the way you have it written. I posted a reply on your other thread hereabout this topic. And maybe re-read Ernest's reply on this thread.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Martin Mathis
Ranch Hand

Joined: Dec 20, 2004
Posts: 45
How about making a method that calculates (and RETURNS) the gross amount, given inputs of hourly rate and hours worked.

Create another method that calculates (and RETURNS) the net amount, given input of the gross amount.

then your calls to them would just look like the following

If it absolutely needs to be one method, consider creating a object or using some sort of structure to return all of the values you want to return.
I agree. Here's the link:
subject: question about variables changed inside methods
It's not a secret anymore!