When to Make a Method Static in Java? Example (original) (raw)

Making a method static in Java is an important decision. Though, static keyword is one of the fundamental concepts, many times programmers get confused to make a particular method static or not. In Java programming, the main motivation for making a method static is convenience. You can call a static method without creating any object, just by using its class name. So if you need a method, which you want to call directly by class name, make that method static. Utility classes e.g. java.lang.Math or StringUtils are good examples of classes, which use static methods. Before making a method static, you should look into the limitations of static methods as well, as you can not override static methods in Java.

By keeping these properties in mind, we can make few rules, which will help to decide when to make a method static in Java and when to use them.

In this Java article, we will learn more about the benefits and limitations of making a method static, and also see a couple of examples of static methods from JDK to learn when and how to use the static methods in Java.

What does the static method do in Java?

When you see a static method in Java code, What do you assume? What reasoning and assumptions does a reader make when he sees a static method? This is important to learn to ensure we are using the static method correctly.

  1. Static method doesn't modify the state of the object. Since the state of an object is maintained as instance variables, and Java doesn't allow non-static variables in the static context. Modern days IDE like Netbeans also shows a static method in italics to differentiate it from other methods.

  2. Static method mostly operates on arguments, almost all static method accepts arguments, perform some calculation and return value.

How to use static method in Java

Rules to make a method static in Java

There are no hard and fast, well-written rules, to decide when to make a method static or not, But there are few observations based upon experience, which not only help to make a method static but also teaches when to use the static method in Java. You should consider making a method static in Java :

  1. If a method doesn't modify the state of the object, or not using any instance variables.

  2. You want to call the method without creating an instance of that class.

  3. A method is a good candidate for being static, if it only works on arguments provided to it e.g. public int factorial(int number){}, this method only operates on the number provided as an argument.

  4. Utility methods are also the good candidates of being static e.g. StringUtils.isEmpty(String text), this is a utility method to check if a String is empty or not.

  5. If the function of the method will remain static across class hierarchy e.g. equals() method is not a good candidate for making static because every Class can redefine equality.

When to use the static method in Java?

How to use static method in JavaNow, we know the benefits and limitations of making a method static in Java, we can see a couple of scenarios where we can use static methods. A factory design pattern provides good use of the static method. You can use the static method to create instances of a class. Even Effective Java book advises about using static factory method, a couple of example of these in Java library is creating thread pool from Executors class.

Executors provides lots of static methods to create different types of thread pool e.g. public static ExecutorService newCachedThreadPool(), public static ExecutorService newFixedThreadPool(int nThreads) etc.

Another interesting use of static methods from JDK is collection classes e.g. Collections and Arrays which provides lot of static utility methods to operate on different kinds of collection.

A static method can also be combined with variable arguments to create a collection of explicitly elements e.g. EnumSet.of(E first, E... rest). Apart from these, if you loot at Apache commons-lang library, you will find a pattern of utils class e.g. StringUtils, ArrayUtils, which provides utility methods to operate on String and arrays.

One more interesting use of the static method I have seen is the valueOf() method inside different value classes e.g. java.lang.String, though this is also an example of the factory method, it's also a nice way to convert one type to another.

For example, valueOf() can also be used to convert String to Integer in Java. In short, it makes sense to use static methods :

  1. Along with creational design pattern e.g. Factory and Singleton.

  2. As utility method, which operates on arguments.

  3. A conversion tool e.g. valueOf().

That's all about when to make a method static in Java. We have seen benefits and limitations of making a method static, and few examples of static methods from JDK. JDK examples will also help you to decide when to use the static method in Java.