Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, is there any disadvantage to static methods on a class?

Lets assume that a rule (or rule of thumb, anyway), has been imposed in my coding environment that any method on a class that doesn't use, modify, or otherwise need any instance variables to do its work, be made static. Is there any inherent compile time, runtime, or any other disadvantage to doing this?

(edited for further clarifications)

I know the question was somewhat open ended and vague so I apologize for that. My intent in asking was in the context of mostly "helper" methods. Utility classes (with private CTORs so they can't be instantiated) as holders for static methods we already do. My question here was more in line of these little methods that HELP OUT the main class API.

I might have 4 or 5 main API/instance methods on a class that do the real work, but in the course of doing so they share some common functionality that might only be working on the input parameters to the API method, and not internal state. THESE are the code sections I typically pull out into their own helper methods, and if they don't need to access the class' state, make them static.

My question was thus, is this inherently a bad idea, and if so, why? (Or why not?)

like image 525
Michael Campbell Avatar asked Mar 18 '10 19:03

Michael Campbell


People also ask

What is the disadvantages of static method in Java?

The static method can not use non static data member or call non-static method directly. this and super cannot be used in static context. Access only static type data (static type instance variable). Call only static method ,if non-static then compile time error.

Is it bad to use static methods in Java?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.

What are the limitations of static methods?

The static method cannot use non-static data member or invoke non-static method directly. The this and super cannot be used in static context. The static method can access only static type data (static type instance variable). There is no need to create an object of the class to invoke the static method.

Is it good to use static methods in Java?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.


2 Answers

The main disadvantage is that you cannot swap, override or choose method implementations at runtime.

like image 130
cherouvim Avatar answered Sep 22 '22 23:09

cherouvim


In my opinion, there are four reasons to avoid static methods in Java. This is not to say that static methods are never applicable, only to say that they should generally be avoided.

  1. As others have pointed out, static methods cannot be mocked out in a unit test. If a class is depending on, say, DatabaseUtils.createConnection(), then that dependent class, and any classes that depend on it, will be almost impossible to test without actually having a database or some sort of "testing" flag in DatabaseUtils. In the latter case, it sounds like you actually have two implementations of a DatabaseConnectionProvider interface -- see the next point.

  2. If you have a static method, its behavior applies to all classes, everywhere. The only way to alter its behavior conditionally is to pass in a flag as a parameter to the method or set a static flag somewhere. The problem with the first approach is that it changes the signature for every caller, and quickly becomes cumbersome as more and more flags are added. The problem with the second approach is that you end up with code like this all over the place:

     boolean oldFlag = MyUtils.getFlag(); MyUtils.someMethod(); MyUtils.setFlag( oldFlag ); 

    One example of a common library that has run into this problem is Apache Commons Lang: see StringUtilsBean and so forth.

  3. Objects are loaded once per ClassLoader, which means that you could actually have multiple copies of your static methods and static variables around unwittingly, which can cause problems. This usually doesn't matter as much with instance methods, because the objects are ephemeral.

  4. If you have static methods that reference static variables, those stay around for the life of the classloader and never get garbage collected. If these accumulate information (e.g. caches) and you are not careful, you can run into "memory leaks" in your application. If you use instance methods instead, the objects tend to be shorter-lived and so are garbage-collected after a while. Of course, you can still get into memory leaks with instance methods too! But it's less of a problem.

Hope that helps!

like image 22
Eric Galluzzo Avatar answered Sep 21 '22 23:09

Eric Galluzzo