In this article, we will take a look at the difference between the static and non-static methods in Java, one of the frequently asked doubts from Java beginners. In fact, understanding static keyword itself is one of the main programming fundamentals, thankfully it's well defined in the Java programming language. A static method in Java belongs to the class, which means you can call that method by using class name e.g. Arrays.equals(), you don't need to create an object to access this method, which is what you need to do to access non-static method of a class.
A static method is treated differently by compiler and JVM than non-static methods, static methods are bonded during compile time, as opposed to binding of the non-static method, which happens at runtime.
Similarly, you can not access non-static members inside static context, which means you can not use non-static variables inside static methods, you can not call non static methods from static ones, all those will result in compile-time error.
Apart from these significant differences between static and non static methods, we will also take a look at few more points in this Java tutorial, with a code example, which shows some of these differences in action.
By the way this is the second article on static method, in first article, we have learned about when to use static method in Java
And, if you want, You can further see these Java courses and books to learn more about Java fundamental concepts like static and non-static.
Similarly, you can not access non-static members inside static context, which means you can not use non-static variables inside static methods, you can not call non static methods from static ones, all those will result in compile-time error.
Apart from these significant differences between static and non static methods, we will also take a look at few more points in this Java tutorial, with a code example, which shows some of these differences in action.
By the way this is the second article on static method, in first article, we have learned about when to use static method in Java
Static vs Non Static method in Java
Let's see a couple of differences between the static and non-static methods in the Java programming language, which is enforced by the language itself.
1) I think first and foremost difference between them is that you can call static method without creating any object e.g. Collections.sort(). This makes static method useful utility, while you need to instantiate an object to call non static method in Java. Static methods are also pretty useful on several design pattern including Factory and Singleton.
2) You can not access a non static variable inside any static method in Java, but reverse is fine i.e. you can access static variables or call static method from a non static method without any compile time error.
3) One more worth noting difference between static and non static method is that you can not override static method in Java. They are bonded during compile time using static binding. Though you can create a similar static method in sub class, that is know as method hiding in Java.
4) static methods are known as class methods, if you synchronize static method in Java then they get locked using different monitor than non-static methods e.g. both static and non static methods are locked using the different monitor in Java, and that's why its grave Java mistake to share a resource between the static and non-static method in Java.
5) Static methods are commonly used inside utility classes e.g. java.util.Collections or java.util.Arrays, because they are easier to access, as they don't need an object. One of the popular examples of static method is though the main method, which acts as an entry point for Java programs.
1) I think first and foremost difference between them is that you can call static method without creating any object e.g. Collections.sort(). This makes static method useful utility, while you need to instantiate an object to call non static method in Java. Static methods are also pretty useful on several design pattern including Factory and Singleton.
2) You can not access a non static variable inside any static method in Java, but reverse is fine i.e. you can access static variables or call static method from a non static method without any compile time error.
3) One more worth noting difference between static and non static method is that you can not override static method in Java. They are bonded during compile time using static binding. Though you can create a similar static method in sub class, that is know as method hiding in Java.
4) static methods are known as class methods, if you synchronize static method in Java then they get locked using different monitor than non-static methods e.g. both static and non static methods are locked using the different monitor in Java, and that's why its grave Java mistake to share a resource between the static and non-static method in Java.
5) Static methods are commonly used inside utility classes e.g. java.util.Collections or java.util.Arrays, because they are easier to access, as they don't need an object. One of the popular examples of static method is though the main method, which acts as an entry point for Java programs.
And, if you like to see the difference in a tabular format for better understanding, here is a nice table which highlights difference between static and non-static method in Java:
Static vs Non Static Method in Java - Example
Here is the Java program to highlight a couple of differences between static and non-static methods in Java :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java program to show some differences between static and non static methods in Java.
*
* @author http://javarevisited.blogspot.com
*/
public class StaticMethodTest {
private static int version;
private String name;
private static final Logger logger = LoggerFactory.getLogger(StaticMethodTest.class);
public static void main(String args[]) {
// You can call static method directly, wihtout creating any object
StaticMethodTest.staticMethod();
// You need an object to call non static mehtod in Java
StaticMethodTest myObject = new StaticMethodTest();
myObject.nonStaticMethod();
}
public void nonStaticMethod(){
logger.info("I am a non static method in Java");
// You can access static variable inside non static method
logger.info("static version from non static method " + version);
}
public static void staticMethod(){
logger.info("I am a static method in Java, version : " + version);
//System.out.println(name); // compile time error
}
}
Output:
2013-07-01 04:10:08,029 0 [main] INFO StaticMethodTest - I am a static method in Java, version : 0
2013-07-01 04:10:08,060 31 [main] INFO StaticMethodTest - I am a non static method in Java
2013-07-01 04:10:08,060 31 [main] INFO StaticMethodTest - static version from non static method 0
That's all on difference between static and non static methods in Java. You can see that non static members like non-static methods and variables are not accessible inside static context and you also need to create an object, before calling a static method, due to this reason static methods are more suited as utility method e.g. Arrays.deepEquals(). In this way, you can call them directly using their class name without worrying about how to create object and how to pass different properties required by constructor of that class. At the same time, you also need to remember that all object will have same value for static members.
nice article.. correction in conclusion part.
ReplyDeleteyou also need to create an object, before calling a static method
correction
you also need to create an object, before calling a non static method
There are both pros and cons of making a method static in Java
ReplyDelete- one of the biggest drawback is that you cannot override a static method, which means you lose the power of Polymorphism.
- but at the same time, you can use static method without creating an instance of the class, which is very convenient in many cases.