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.
Joined: Jan 14, 2006
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
author and iconoclast
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.
I'm getting "cannot find symbol" in processPay() and printEmployeeInfo()
I am lost in this code...
Joined: Feb 03, 2005
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...
Joined: Jan 17, 2006
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
Joined: Dec 20, 2004
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.