Static vs Non-static Methods in Java

  • Saturday, 23 July 2016
  • 540 times
Today i stumbled over this explanation of the diference of static vs non-static methods, and even though I'm not a java programmer i think it's one of the best i've seen, short and clear while at the same time exposing some of the advantages and implications of choosing one or another. Read the full article here.
/**
 * Java program to show difference between static and non-static method.
 * It explains how calling static method and changing static variable
 * affect all objects, instead of just calling object in case of non-static
 * method
 * @author WINDOWS 8
 *
 */
public class StaticDemo{

    public static void main(String args[]) {
        Player p1 = new Player(10, 10);
        Player p2 = new Player(20, 20);
     
        // calling non-static method move() on p1
        p1.move();
     
        // let's print position of p1 and p2
        // only p1 should have moved, no impact on p2
        System.out.println("P1 : " + p1);
        System.out.println("P2 : " + p2);
     
        // calling static method on p2
        p2.changeGame("Cricket Brian Lara 1997");
     
        // should have affected both p1 and p2
        System.out.println("P1 : " + p1);
        System.out.println("P2 : " + p2);
     
    }
 
 
 
}
/**
 * Simple Java class to represent a Player with position.
 * It has both static and non-static method to operate on
 * player objects.
 * @author WINDOWS 8
 *
 */
class Player{
    private static String game = "Super Mario Bros";
 
    private int X;
    private int Y;
 
    public Player(int x, int y){
        this.X = x;
        this.Y = y;
    }
 
    public void move(){
        X++;
        Y++;
    }
 
    public static void changeGame(String nextGame){
        game = nextGame;
    }
 
    @Override
    public String toString() {
        return String.format("Game: %s, X : %d, Y: %d ", game, X, Y);
    }
}

Output
P1 : Game: Super Mario Bros, X : 11, Y: 11
P2 : Game: Super Mario Bros, X : 20, Y: 20
P1 : Game: Cricket Brian Lara 1997, X : 11, Y: 11
P2 : Game: Cricket Brian Lara 1997, X : 20, Y: 20


Properties of Static Methods in Java

Static is not just another keyword it's one of most important programming concept and every language has some specific features related to static variables, methods, and classes. Though key difference comes from the fact that static is something which is associated with the class rather than an instance, there are many subtle details of static keyword, which you should keep in mind. Let's see some of them here.

1) You cannot use this keyword inside a static method in Java. Since this is associated with the current instance, it's not allowed in static context because no instance exist that time. Trying to use this variable inside static context e.g. static block or method is compile time error in Java.

2) super keyword is not allowed inside the body of a static method or static block. Just like this, a super keyword is also used to represent current instance of the parent class and since the instance is not valid in static context, it's a compile-time error to use super keyword inside a static method in Java.

3) You cannot use any instance variable inside a static method, they will result in compile time error e.g. "non-static reference is not allowed inside static methods". Why? because instance may not exist when the static method gets called. They are not associated with any instance, you can call them even on a null variable without throwing NullPointerException in Java. In short, you cannot use  non-static members inside your static methods in Java.

3) A static method cannot be overridden in Java, Why? because they are not resolved at runtime, which is the case with overriding. Like a private and final method, static methods are also resolved at compile time. By the way, this doesn't mean that you cannot declare another static method with same name and signature in the child class, you can do so and the compiler will not complain at all, but that would not be method overriding, instead that is called method hiding in Java. This new method will hide the static method from the parent class and when you call it using Child class only.  See why static method cannot be overridden for more details.

4) Static method invocation is more efficient than instance method because the compiler doesn't need to pass this reference, unlike instance methods. That's why you should make utility method as static if they are not using any instance variable.

5) Static methods are bonded during compile time, as opposed to an instance method, which is resolved during runtime; former is known as static binding while later is known as dynamic binding in Java. See the difference between static and dynamic binding for more details.

6) By the way, You can overload static methods in Java, there is no restriction on that from Java language perspective. In fact, JDK API itself has lots of overloaded static method e.g. String.valueOf() is overloaded to accept an intlongfloatdouble, and boolean data type. EnumSet.of() is also overloaded to create EnumSet with one, two or multiple values.

7) You can call a static method without creating any instance of that class, this is one of the biggest reason of making a method static in Java. For example, to call Double.valueOf(), you don't need to create an instance of the Double class. You can even call static methods with reference variable having a null value and to your surprise, those will not result in a null pointer, as only the classname is used to find and call static methods. For example, following code will run fine in Java.
Double d = null;
d.valueOf(2);

8) A static method can be synchronized, and more importantly, they use different lock than instance synchronized method. Which means, two threads can simultaneously run a static and a non-static method in Java. This is a common multi-threading mistake in Java and you should look for that while writing Java test involving code and multiple threads. To learn more see the difference between static and non-static synchronized method in Java.

9) You cannot declare a static method inside the body of an interface in Java.

10) As the name implies, instance methods require an instance of a class. Static methods (and fields) are independent of class instances, as they are only associated with class. To use instance methods, you'll have to create an instance using new. A static method is considered to have no side effect on the state of an object because of the said reason.


When to use Static method in Java

Methods which never need to access instance variables are good candidate to become static. The functions in java.lang.Math are a good example as they only need input arguments. One rule-of-thumb, if you have a class MyClass,  ask yourself  "does it make sense to call this method, even if no Object of MyClass has been constructed yet?" If so, it should definitely be static. Another simple rule in Java is,  Immutable is good. So if your method does not change the state of the object and if it must not be overridden by a subclass, it should be static. Also static methods are easier to inline by the JIT-Compiler. Let's see an example, supper you have a class Car and you might have a method double convertMpgToKpl(double mpg) , this is another good candidate to become static, because one might want to know what 45mpg converts to, even if nobody has ever built a Car. But void setOdometer(double mi) (which sets the distance-travelled for one particular Car) can't be static because it's inconceivable to call the method before any Car has been constructed. In short, If a method inside a class is not using instance variable, then it's the good candidate for making it static, and may be it might not belong to that class itself. Here are couple of common scenarios in Java world, where use of static method is considered appropriate :

1) Use static method as static factory method for data conversion e.g. Integer.valueOf(), which is used to convert any other data type to integer e.g. Integer.valueOf(String str) converts String to Integer in Java.

2) Use static method as utility methods e.g. methods from Math class or Arrays or Collections e.g. Collections.sort() method.

3) getInstance() method of Singleton design pattern is another popular example of Static method in Java

4) Pattern.compile() is another static method, which is example of static factory method as discussed in the first example

5) Hmm, how can I forget the most popular example of a static method in Java,  yes I am talking about public static void main(String[] str). The main method is one of the most known examples of static methods, in fact, why main is static in Java, is also one of the frequently asked Java question on college viva or on Java interviews.


Disadvantage of Using static method in Java

There is nothing in the world which only has pros without any cons, and static methods are no different. Though they are immensely helpful from convenience perspective and critical to implement some core Java design patterns like Singleton and Factory Pattern, they also have some serious drawbacks. Here are a couple of them :

1) No Runtime Polymorphism
First and foremost, the static method reduces the flexibility of your system. Once a method is declared static, it's fixed. You cannot provide an alternate implementation of that method in a subclass, as the static method cannot be overridden, they can only be hidden. Even this method hiding can cause serious and subtle bugs.

2) Potential of Concurrency Bugs
One of the not-so-obvious thing about static synchronized method is that it uses different lock than instance synchronized method. which means, If you are sharing a resource between a static and non-static method, and thinking that you have synchronized their access, you are living on a dream. Both methods can be run in parallel at the same time and potentially cause subtle concurrency bugs.

3) Lose of interface
You cannot declare a static method inside interface in Java, so if your design has a static method, you cannot use interface. You need to put your static method only on implementations, which means all code, which is written against interface will not have access to that static method. Since programming to interface than implementation is one of the popular object-oriented design principles, you will see a lot of code written like that.


That's all about the difference between the static and non-static method in Java. We have learned key points about static methods, have seen some popular examples of them, explored when to use static methods and also learned about the potential disadvantage of using static methods. This gives you a fair idea about capability and limitation of static methods in Java programming language. Careful and clever use of this concept can result in more readable and concise code while misuse can cause a lot of pain especially in areas of concurrency, flexibility, and maintainability.

Read more: http://www.java67.com/2014/10/difference-between-static-and-non-static-method-java-programming.html#ixzz4FFn7IrZC
akanerd

O administrador!
Pessoa extremamente inteligente, possui o dom da ubiquidade.

Login to post comments

Subscreva a Newsletter

maker.akanerd.com

    • Neste site, procura-se documentar alguns projectos do seu mentor, projectos esses, relacionados com áreas como a electrónica, a automação industrial, a informática, desenvolvimento web, sistemas operativos e redes de computadores, entre outros. Conta também com uma área educacional (akanerdemy), onde se apresentam video tutorias nas referidas áreas.