aspose file tools*
The moose likes Beginning Java and the fly likes How to decide that method shoukld be static or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to decide that method shoukld be static or not?" Watch "How to decide that method shoukld be static or not?" New topic
Author

How to decide that method shoukld be static or not?

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I have a method which has following signature



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.


My Blog SCJP 5 SCWCD 5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks Ankit.

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 ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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 hope now you are clear...
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks.

this means that it doesn't matter that the method is static or not as the local variables are not static and each method invocation will retain its own copy of local variables being used.

But this line is confusing me:
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

Can you explain this line too.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

look at this example

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

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 ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

You didn't pay attention to one of the words

//here you can safely use increment and finalPay but not basicPay and grossPay as they are class level fields
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

No I read that.

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.

Please correct if I am wrong.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

when I said this

//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 ...
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I have no idea about concurrency issues. Haven't seen them till now? not encountered by such problems yet?

Are you talking about two threads accessing the same object and thus modifying its instances values(when one thread is sleeping or waiting) or something else ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Originally posted by Himanshu Gupta:
I have no idea about concurrency issues. Haven't seen them till now? not encountered by such problems yet?

Are you talking about two threads accessing the same object and thus modifying its instances values(when one thread is sleeping or waiting) or something else ?


Yes I am talking about two threads concurrently accessing and modifying the value of a field...
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks Ankit. At last the query is solved.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to decide that method shoukld be static or not?