This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Methods" Watch "Static Methods" New topic
Author

Static Methods

Raj Joe
Ranch Hand

Joined: Oct 15, 2004
Posts: 41
I have a class which has all static methods.

One I Want to now how they work as i don't instaintiate this class I just call the static methods with the class name.

Two what will happen if 100 users are calling on the same static method at the same time.(Is it FIFO or what?)
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
That really depends on the algorithm used by JVM. I don't think its specified in the specs on whether it'll be FIFO or round robin.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Raj Joe:
One I Want to now how they work as i don't instaintiate this class I just call the static methods with the class name.
Static methods do not require an instance of the class to run, and they don't have access to an implicit "this" local variable.

An example wuold bethat converts the primitive int i to a String: Integer.toString(123) returns "123". Since ints aren't objects (they have no fields or methods), a static method in the Integer class provides the service.

Originally posted by Raj Joe:
Two what will happen if 100 users are calling on the same static method at the same time.(Is it FIFO or what?)
As long as the method is not synchronized, any number of threads can be executing it simultaneously. If it is synchronized, each thread will execute it in some sequence determined by the JVM's thread scheduler.
Raj Joe
Ranch Hand

Joined: Oct 15, 2004
Posts: 41
Originally posted by David Harkness:
that converts the primitive int i to a String: Integer.toString(123) returns "123". Since ints aren't objects (they have no fields or methods), a static method in the Integer class provides the service.

As long as the method is not synchronized, any number of threads can be executing it simultaneously. If it is synchronized, each thread will execute it in some sequence determined by the JVM's thread scheduler.


Assume I have an abstract class with all static methods should I ynchronize all my static methods.What will happen if I don't?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Assume I have an abstract class with all static methods should I synchronize all my static methods. What will happen if I don't?

There are other ways besides using synchronized to make a static method thread-safe, but they are more complicated. For instance, you generally should use local variables instead of class variables to be sure each thread gets its own copy. You also have to consider any calls to methods in the java library classes found in java.lang, java.io, etc. Some of these methods are inherently thread-safe, some use synchronized, and some are not thread-safe.

The consequence of having multiple threads calling a method that is not thread-safe is the random occurrance of incorrect results depending on when each thread gets control. If three threads are each incrementing a shared variable, you could wind up adding one, two, or three to the variable. Remember that increment is implemented as separate load, add 1, and store operations. You could get load(thread 1) add1(1) load(2) add1(2) load(3) add1(3) store(1) store(2) store(3). Think about all the combinations and the answers you would get.


Mike Gershman
SCJP 1.4, SCWCD in process
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

an abstract class with all static methods


If you have that, you have something very broken.

A class with all static methods should certainly be declared final and expose no public or protected constructors.
This is in direct contrast to what an abstract method is intended for (and the compiler won't allow an abstract final class).


Tony Morris
Java Q&A (FAQ, Trivia)
Raj Joe
Ranch Hand

Joined: Oct 15, 2004
Posts: 41
Originally posted by Tony Morris:


If you have that, you have something very broken.

A class with all static methods should certainly be declared final and expose no public or protected constructors.
This is in direct contrast to what an abstract method is intended for (and the compiler won't allow an abstract final class).


Is there any disadvantage in terms of performance when declaring lots of static variable and methods
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Is there any disadvantage in terms of performance when declaring lots of static variable and methods

On the contrary, using only static class members eliminates the overhead of creating and tracking class instances and the memory occupied by each instance. If there are no instance variables, the methods had might as well be static as any objects will not retain any state between method calls.

However, programming without any objects is not Object Oriented Programming, it's modular programming using an object oriented language.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Methods
 
Similar Threads
synchronized keywords in static methods
static inner class as cache ?
Static Methods
Difference between Hidden and Overridden
A big confusion