Question.. if I make a method static... and I'm changing values that are passed in from the user, do I have a danger of hitting someone else’s values and changing them instead since it is not Synchronized?
By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Dale It doesn't look like you're changing any variables in the method. It gets a double and returns a String, so I dont think there si any danger of you changing something it shouldn't. What were you worried about changing? If it is multi-threaded each thread will have its own call stack and local variable so nothing there should effect another thread either. Unless the double variable passed in is static too then it might change in some outside method or bysome other means but that wouldn't effect the method and the method wont effect the variable it just means that the metod may return a formatted value that no longer matches the current value of the double. IF I misunderstood let me know...
Joined: Nov 02, 2000
So in other words, since I own the variable that is passed in (its on my stack), I don't have to worry about it being changed. The only time I need to worry about over writing something is when the variable is static and belongs to everyone.. correct?? Dale
Joined: May 10, 2001
Dale, that is correct. However, you would also have to worry about it with a non static variable in a multi-threaded app. If for example you have an instance variable that is sent to a method in two threads, each thread could change the variable. Keep, in mind that this really only applies to reference variables not primitives as they are passed by value anyway. I said it didn't matter in your code because: a. you didn't change the value of amount and b. it is a primitive so any changes you did make would be local to the method anyway. If, on the other hand, you were passing in a Double (reference not primitive) and you did cvhange it then it could effect other threads trying to use it. With a static variable you can change it in a method if you use it directly (as opposed to passing it in):
In the first method you are changing the value fo the static variable so it can have an impact on other threads. In the second method you are only changing the value of the local variable x so multi-threaded apps wont be effected. hope I didn't make this too basic for you - but someone else might need a more fundamental explanation - feel free to skip the parts you know