This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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
posted
0
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?
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.
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).
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
posted
0
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.