wood burning stoves 2.0*
The moose likes Java in General and the fly likes To Static or Not To Static... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "To Static or Not To Static..." Watch "To Static or Not To Static..." New topic
Author

To Static or Not To Static...

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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)
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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...


Dave
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To Static or Not To Static...
 
Similar Threads
Injection problem - not quite sure if this is something i should be using injection for
Getting Image height in a JSP
How to convert hours,minutes,seconds to seconds
using rational class for summation series
Help with Java Mortgage Program