When to make a method static in Java
Making a method static in Java is an important decision . Though, static keyword is one of the fundamental concepts, many times programmers gets confused to make a particular method static or not. In Java programming, main motivation for making a method static is convenience. You can call a static method without creating any object, just by using it's 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 uses static methods. Before making a method static, you should look into limitation of static methods as well, as you can not override static method 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 benefits and limitation of making a method static, and also see couple of examples of static methods from JDK to learn when and how to use static method in Java.
What does static method do in Java
When you see a static method in Java code, What do you assume? What reasoning and assumptions a reader make, when he sees a static method? This is important to learn to ensure we are using static method correctly.
1) Static method doesn't modify state of object. Since state of object is maintained as instance variables, and Java doesn't allow non static variables on static context. Modern days IDE like Netbeans also shows 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.
Rules to make a method static in Java
There is 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 static method in Java. You should consider making a method static in Java :
1) If a method doesn't modify state of object, or not using any instance variables.
2) You want to call method without creating instance of that class.
3) A method is good candidate of being static, if it only work on arguments provided to it e.g. public int factorial(int number){}, this method only operate on number provided as argument.
4) Utility methods are also good candidate of being static e.g. StringUtils.isEmpty(String text), this a utility method to check if a String is empty or not.
5) If function of method will remain static across class hierarchy e.g. equals() method is not a good candidate of making static because every Class can redefine equality.
When to use static method in Java

1) Along with creational design pattern e.g. Factory and Singleton.
2) As utility method, which operate 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 limitation of making a method static, and few examples of static methods from JDK. JDK examples will also help you to decide when to use static method in Java.
Mentioned books in this article
Effective Java 2nd Edition from Joshua Bloch
No comments:
Post a Comment