Friday, 21 April 2017

Difference between Static vs non Static method in Java



Difference between Static vs non Static method in Java
One of the key difference between a static and a non-static method is that static method belongs to a class while non-static method belongs to the instance. This means you can call a static method without creating any instance of the class by just using the name of the class e.g. Math.random() for creating random numbers in Java. Often utility methods which don't use the member variables of the class are declared static. On the other hand, you need an instance of the class to call a non-static method in Java. You cannot call it without creating an object because they are dependent upon the member variables which has different values for different instances.  One more important difference between the static and non-static method is that you cannot use a non-static member variable inside a static method, you cannot even call a non-static method from the static method, but the opposite is true e.g. you can call a static function from a non-static method in Java.


The static is one of the concept, which always create some doubt on Java programmers mind, especially beginners, hence it's very important for them to read a book which explains these fundamentals in detail.

The Core Java Volume 1 - Fundamentals by Cay S. Horstmann is one of such book and if you haven't read it yet, you should check it. One of the better books to learn Java fundamentals.
difference between static vs non-static method in Java


Difference between Static and Non-Static Method in Java

Now, let's see some more differences between a static and non-static method in Java. There are a lot of implication of making a method static in Java e.g. you cannot override static methods, which we will see in this part of the article.

1) Static method cannot be overridden
Yes, this is another key difference between a static and non-static method. You can override a non-static or instance method but the static method cannot override in Java. Though, when you declare the same static method in the subclass, it hides the method from the superclass, also known as method hiding in Java. See this article learns more about overriding and static in Java.


2) You cannot use this and supper inside static method
Both this and super are associated to an instance and because you cannot use a non-static context inside a static method in Java, you cannot use both this and super inside the static method as well.


3) You cannot use non-static variables e.g. instance variable inside static method
Apart from this and super, you cannot use any non-static or instance member variable inside a static method in Java. It will result in compile time error as shown in the following code:


4) You can call static methods on null references
This is one of the hidden detail of Java programming language that you can call a static method on null reference and code will not throw NullPointerException. This happens because a static method is bonded at compile time by using the class name only.

/**
 * Java Program to demonstrate difference between static
 * vs non-static methods
 * 
 * @author WINDOWS 8
 *
 */
public class TestString {
 
    public static void main(String[] args) {
        
        TestString t = null;
        t.display(); // no null pointer exception       
    }
     
    public static void display(){
        System.out.println("Inside static method");
    }
    
    public void show(){
        System.out.println("Inside non-static method");
    }
    
}
 
Output
Inside static method

5) A static method is bonded during compile time.
This is true and that's why you can call a static method without creating any instance. Due to this property, you can also overload a static method in Java but you cannot override it, which potentially require runtime binding.


6) When to use static and instance method in Java
Both utility and factory methods are good candidates for making static because they don't have any side-effect and doesn't require any data outside of method arguments. On the other hand, instance method usually makes change on object's state by modifying member variables value.


7) The static method usually operates on function arguments passed to them.
The functions in java.lang.Math is a good example as they only need input arguments.  Java's built-in methods Arrays.sort() and Collections.sort() are good examples of static methods. On the other hand put() and get() method of HashMap are a good example of instance or non-static methods.


8) Static methods belong to class.
Static methods belong to the class, so can be called without a specific instance of that class needed. For example, the Math class contains many static methods which mean you can use these methods without needing to instantiate the Math class. Also, the main method is usually static because it is called first before there is any time to instantiate a class!

Non-static methods can only be called on an instance of the class itself, and they usually do something that depends on the individual characteristic of the class (e.g. play with variables).


9) You cannot call non-static method from static method in Java
Yes, this is true, trying to do so will result in compile time error but opposite is true i.e. you can call a static method from a non-static method without any issue as shown in following code example:
cannot call a non-static method from static in Java



That's all about the difference between the static and non-static method in Java. It's one of the fundamental concepts in Java and every Java Programmer should know the rules of static methods and understand when to make a method static in Java. General guideline is to use a static method for utilities, conversion, and factory methods. You can also make a method static if it doesn't have any side-effect on an object.



No comments:

Post a Comment