Jose Sandoval Google
 Resume     Software     Writing     Drawings     Photos     Home Search WWW Search josesandoval.com


Using static or instance methods


ow do you know which type of method to code and use? I usually code static methods when they don't need instance variables or it makes no sense for a whole object to exist. I.e. Quick transformation methods and the java.lang.Math class uses them all over the place.

But, you would argue, if I use a static method then I'm not doing Object Oriented Programming.

Well, it theory it isn't. In the OO paradigm an object is an instantiation of a class and a class is an ADT that supports inheritance and an Abstract Data Type is defined as a data type together with actions to be performed on an instance of the data type.

Hence, the statement java.lang.Math.sin(0.5) in your program is not really OO Programming, as there is no instance of the Math class (or is there?). However, it isn't wrong to use this type of methods and you shouldn't feel bad about coding static methods.

The one concern I had was regarding performance. The question I always had was "which type of method implementation performs better?" I had to find out, and of course I wrote the following quick little test:

public class Run {
    public static void main(String[] args) {
	int result = 0;

	// Static call
	long t1 = System.currentTimeMillis();
	for (int i = 0; i<100000; i++) {
	    result = Item.run();
	}
	long t2 = System.currentTimeMillis();

	System.out.println("Item.run() time: " + (t2 - t1) + " ms.");

	// Instance call
    	long t3 = System.currentTimeMillis();
	for (int i = 0; i<100000; i++) {
	    result = new Item2().run();
	}
	long t4 = System.currentTimeMillis();

	System.out.println("new Item().run() time: " + (t4 - t3) + " ms.");}
}

class Item {
    public static int run() {

return (1 + 1); } } class Item2 { public int run() { return (1 + 1); } }
A typical result looks like:
Item.run() time: 30 ms.
new Item2().run() time: 351 ms.
What to gather from the results
You can make your own conclusion as to which type of method to code.

But, with this little experiment, the static call runs much faster than the instance method call. For obvious reasons: inline methods run a bit much faster, as there is no run time method binding. Also, we hear that object creation is expensive, and it is, plus you add the late method binding and there is more to do.

However, this result doesn't mean that object instantiation needs to be avoided or replaced with inline method calls. It usually doesn't get you order of magnitude performance gains, hence, performance experts suggest to use inline method definition sparingly.

Now, back to static vs. instantiated method calls. I code them all the time and usually put them in utility classes. I.e. Date transformation routines, which don't belong anywhere else.

Do you use static methods?

I almost forgot
In Java, you can define a method static and still be able to call the method through an instance of the object (Provided, your constructors are not defined private - Nifty trick to avoid instantiation of some classes).

Guestbook
Copyright © Jose Sandoval 2005 - jose@josesandoval.com