• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about variables changed inside methods

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this work?

 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you try to compile and run it?
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


Regards
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



or



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.
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting "cannot find symbol" in processPay() and printEmployeeInfo()



I am lost in this code...
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic