It makes a connection to the server and sens a post request. Now my doubt is if the method is static and it is currently executing the code means making a connection and during that process another class tries to access it then what will happen?
I also want to know that if the method is not static then how is this problem solved as I think that there is only one copy of method. So how the JVM manages this?
I know that it is a silly doubt but without knowing this i am confused a lot.
Well Himanshu whether the method is static or not, it doesn't matter a lot when multiple threads are executing the method concurrently. The matter of concern is are you using any class lever fields.
Suppose you keep the method static. Now if your method uses some static fields of the class, then multiple threads executing the code concurrently will create problems as they will share the same fields.
Now let's say you keep the method non-static. If the method still uses some static members, then again you can run into troubles. If your method is only using instance fields, then you are fine unless two threads are going to execute the method on the same object in which case you will again run into troubles.
If your method uses only local variables, then you will be fine in both the cases as local fields are created on every invocation of the method so no two invocations will share the variables i.e. two invocations cannot interfere with one another...
My concern is that if I declare the method as static then all the variables declared inside that method will become static too. So when this method will be called, at that time they will get their values from the arguments. Now when this static method is getting executed at that time another thread tries to run it and will again initialize those static variables with some other values and hence it will throw an exception or it will break the application. (Synchronization can solve this issue)
The case you were talking about that if no static local members are there and method is also not static then it is OK but then the question comes that being a helper method which does not have to do any thing rather posting a request; it doesn't seems good to make multiple objects of that class just to make use of that method.
So I simply want to know the deciding factor which should be taken care of while making a function static or not? [ December 09, 2008: Message edited by: Himanshu Gupta ]
Originally posted by Himanshu Gupta: if I declare the method as static then all the variables declared inside that method will become static too.
You are confused here. variables in methods are not static or instance. They are just local. So whether they are declared as parameters or are declared inside the method, each invocation of the method will have a separate copy of them. So you don't have to worry about synchronization in that case. You have to worry about synchronization only in these cases
-static method uses static fields of the CLASS -non-static method uses static fields of the CLASS -non-static method uses non-static fields of the CLASS and there is a possibility that multiple invocations can be made from the same object concurrently
I think you typed it wrong or something else happened. We can surely use basicPay and grossPay in function method(int increment) as I have shown in following code. I compiled it, so it is fine and legal.
And I was not able to relate my confusion with this example.
Thanks for your help Ankit. [ December 09, 2008: Message edited by: Himanshu Gupta ]
Even if we may have some controversies(in designing or logic) with static fields being used in non static methods but what is the problem in using non-static members in non-static methods. I think that is perfectly legal and that's the way we make use of instance variables, accessing them through their getter and setters which are nothing but methods.
//here you can safely use increment and finalPay but not basicPay and grossPay as they are class level fields
I meant that you can use increment and finalPay without worrying for concurrency issues but while using basicPay and grossPay, you will have to worry about concurrency issues. I know that the method can use instance fields. I wrote that sentence as I thought you would understand what I meant to say. I was a bit lazy at the keyboard at that time ...