7/15/13
When to make a method static in Java
Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL
Java Java
W EDNES DA Y , J ULY 10, 2013
Download Java JDK
Java Developer
Java Help
Searc h
When to make a method static in Java
Recent Posts
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. j a v a . l a n g . M a t hor S t r i n g U t i l s , 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.
When to make a method static in Java Top 5 JQuery books for Beginners and Web developers - Best of lot How SSL, HTTPS and Certificates works in Java web applications Role based Access control using Spring Security and MVC, Mapping LDAP Groups to Authorities for Authorization 10 Java Exception and Error Interview Questions Answers 5 must read books to learn Object Oriented and Java Design patterns - Best of lot
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. p u b l i ci n t f a c t o r i a l ( i n tn u m b e r ) { } , this method only operate on number provided as argument. 4) Utility methods are also good candidate of being static e.g. S t r i n g U t i l s . i s E m p t y ( S t r i n gt e x t ) , 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
Now, we know the benefits and limitation of making a method static in Java, we can see couple of scenarios where we can use static methods. Factory design pattern provides a good use of static method. You can use static method to create instance of a class. Even Effective Java advises about using static factory method, couple of example of these in Java library is creating thread pool from Executors class. E x e c u t o r sprovides lots of static methods to create different types of thread pool e.g. p u b l i cs t a t i cE x e c u t o r S e r v i c e n e w C a c h e d T h r e a d P o o l ( ) , p u b l i cs t a t i cE x e c u t o r S e r v i c en e w F i x e d T h r e a d P o o l ( i n tn T h r e a d s )etc. Another interesting use of static methods from JDK is collection classes e.g. C o l l e c t i o n sand A r r a y swhich provides lot of static utility methods to operate on different kinds of collection. Static method can also be combined with variable arguments to create a collection of explicitly elements e.g. E n u m S e t . o f ( Ef i r s t ,E . . .r e s t ) .Apart from these, if you loot at Apache commons lang library, you will find a pattern of utils class e.g. S t r i n g U t i l s ,A r r a y U t i l s , which provides utility methods to operate on S t r i n gand arrays. One more interesting use of static method I have seen is v a l u e O f ( ) method inside different value classes e.g. j a v a . l a n g . S t r i n g , though this is also an example of factory method, but it's also a nice way to convert one type to another. For example v a l u e O f ( )can also be used to convert String to Integer in Java. In short, it make sense to use static methods : 1) Along with creational design pattern e.g. Factory and Singleton. 2) As utility method, which operate on arguments.
Follow Us
Follow @javinpaul 3,239 follow ers
Javarevisited on
Follow
+3,257
javarevisited.blogspot.in/2013/07/when-to-make-method-static-in-java.html#more
1/3
7/15/13
3) A conversion tool e.g. v a l u e O f ( ) .
When to make a method static in Java
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
Other Java programming articles for Java programmers Can we overload and override static methods in Java Difference between Singleton and Static Class in Java How to use Java 5 static import feature in your program Difference between static and dynamic binding in Java Difference between Inner class vs static class in Java
Javarevisited
Like 4,660 people like Javarevisited.
Google AdWords India
AdWords.google.com Connect With Potential Customers When They're Searching For You!
Please share with your friends if like this article
F acebook social plugin
Recommendd Books Examples
You might like:
Online Learning
Hierarchies
Programming Software
Programm
Java Programming and Design Pattern Spring Framework Subscribe by email: Subscribe By Javin Paul Subscribe To This Blog Free
Can we Overload and Override Static methods in Java - Interview Question 10 Java Exception and Error Interview Questions Answers How to Swap Two Numbers without Temp or Third variable in Java - Interview Question 10 Exception handling Best Practices in Java Programming
Recommended by
Posts Comments Followers
Posted by Javin Paul at 5:38 AM Labels: coding, core java, Java Programming Tutorials
+15 Recommend this on Google
Observations Online Learning
Designing
Learning
Environmentally Friendly
Langs
Examples Observations
Join this site
w ith Google Friend Connect
Hierarchies
Programming Software
Programm
Members (1250) More
No comments: Post a Comment
E n t e ry o u rc o m m e n t . . .
Already a member? Sign in
Comment as: Google Account Publish Preview
Learn Java Programming Java J2EE Developers Java Sample Programs
Blog Archive 2013 (97) July (4) When to make a method static in Java Top 5 JQuery books for Beginners and Web developer... How SSL, HTTPS and Certificates works in Java web ... Role based Access control using Spring Security an... Home Older Post June (9) May (14) April (18)
Subscribe to: Post Comments (Atom)
javarevisited.blogspot.in/2013/07/when-to-make-method-static-in-java.html#more
2/3
7/15/13
When to make a method static in Java
March (16) February (18) January (18) 2012 (218) 2011 (145) 2010 (33)
References Java API documentation JDK 6 Spring framework doc Struts ANT Maven JDK 7 API MySQL Linux Eclipse Copyright by Javin Paul 2012. Powered by Blogger.
About Me
Privacy Policy
javarevisited.blogspot.in/2013/07/when-to-make-method-static-in-java.html#more
3/3