File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Methods" Watch "Static Methods" New topic

Static Methods

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

This is the method

public static float compute(float a, float b) {
// Some calculations here that take a long time.
// We are using a lot of local variables here.

return someValue;

- If two different threads are accessing compute method and enter the method at the same time, is there a chance of thread 1 not getting the perfect results for the values it has passed or vice versa. (I believe it should not, wish to confirm.)

- How is the memory allocated in Java Heap for a static method. We know that every method runs has its own stack when we do not have static. But what when the method is static ?


Nitin Dubey
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Every method has its own stack frame for parameters and local variables. Methods are therefore thread safe. Notice I have not qualified that for static or non-static because it's always true, no matter what.

A static method does not get an implicit object reference (this refrence) but it has no direct relevance to thread safety or heap object allocation.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
to be honest i don't think Rick is right ...

static methods too are also have to be syncronized if you(programmer) think that other thread will also access that static method for its calculation and that has some storng effect on whole programe if we don't syncornized...
see here we syncronized just because we don't want two thread fight each other for method to complete...
if we don't put syncronized on method that has some strong effect ..
two thread run the method in turn without completing to its end...
till one thread run that method fully...

any other explanations... ??
[ June 20, 2005: Message edited by: amit taneja ]

Thanks and Regards, Amit Taneja
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

Thanks for your replies guys, I am still not sure about this. Ya we do synchronize the things when we want only 1 thread to execute a portion of the code. But what about a situation when you have a static method with local variables ? How is the memory allocated ?

Joji Doi

Joined: Jun 23, 2005
Posts: 3

If the computation involves with member variable(s), then the synchronization needs to be taken into account. If the computation involves only with those premitive types passed from caller and any number of local variables, there is no need of synchronization.

Unless local variable references to an instance which contains a static member variable and this value is modified in the process. Then this critical section might be synchronized.

Cheers! _Joji
shetal bansal
Ranch Hand

Joined: May 09, 2005
Posts: 63
Can you please explain the above text with an example!!
I agree. Here's the link:
subject: Static Methods
It's not a secret anymore!