Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Methods

 
Raj Joe
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Raj Joe
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic