3

Why does not Java support dynamic polymorphism for static methods? If the answer is "static methods are not supposed to be called on instances and hence method call is not needed to be resolved at runtime", then further question is 'Why does Java allow me to call static methods on instances?'. Why doesn't it simply block the user from calling the method on instances directly by giving some compile time error.

Otherway round, What would have gone wrong if Java would have supported Runtime Polymorphism for static methods?

  • It simply makes no sense. Since the class is determined at compile time, there is no potential for polymorphism. And calling a static method on an instance is a bit of (stupid, IMO) compiler slight-of-hand. What you're really doing is calling the static method on the declared class of the reference variable -- which is determined at compile time. – Hot Licks Jul 20 '14 at 21:45
  • 1
    I do agree with what you are saying, but my query is why not to block the functionality which you don't want user to use. – Tushar Baviskar Jul 20 '14 at 21:47
  • 1
    Smalltalk does support this, so it is perfectly possible to implement. I think it was only one of the design decisions made for java and to be honest I never missed it. – Seb Jul 20 '14 at 21:56
  • "then further question is 'Why does Java allow me to call static methods on instances?'" It was a mistake to allow this syntax, but it can't be changed now because it's a breaking change. – Powerlord Aug 08 '20 at 04:46

4 Answers4

7

Why does Java allow me to call static methods on instances?

Your assumption is wrong. It never calls on the instance of the class. It always called on class.

Try below sample code and you will never get NullPointerException

class ABC {
    public static void hello() {
        System.out.println("Hello");
    }
}

ABC abc = null;
abc.hello(); 

What would have gone wrong if Java would have supported Runtime Polymorphism for static methods?

Polymorphism comes into picture when you override the method in subclass. since static method belong to class hence there is no meaning of overriding static methods. Hence Polymorphism always works for instance methods only that belongs to the instances of the class.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • I am assuming, you have instance (instantiated the class), to call static method on. – Tushar Baviskar Jul 20 '14 at 21:35
  • 1
    I don't any actual object of the class. It's a `null` reference. – Braj Jul 20 '14 at 21:36
  • 2
    While this is all true; it would be possible to construct a self consistent inheritance model that would work for static methods (and could under certain circumstances be useful - e.g. every class extending `Tile` has a static `initialiseGraphicsForClass` method and you pass a class to a method). So the question remains; why not? – Richard Tingle Jul 20 '14 at 21:39
1

Static methods are resolved based on a variable's type and not the instance's class. This allows for some optimisations, since the exact method to be called is always known at compile time. Allowing polymorphic static methods would prevent this.

A result of allowing static methods to be called on instances is the following.

class A {
    static void func() {}
}

class B extends A {
    static void func() {}
}

B b = new B();
A a = b;

b.func(); // calls B.func()
a.func(); // same instance, but calls A.func()

Highly confusing, and counter intuitive. Based on how static methods were implemented allowing static methods to be called on instances was a major design flaw and should always be avoided.

By definition static methods do not require an instance to be called. By allowing polymorphic calls you require an instance, and if you require an instance to determine which method to call then why is the method static?

Dunes
  • 37,291
  • 7
  • 81
  • 97
  • 2
    I really am not in favor of supporting dynamic polymorphism for static methods, but if java supports static method call invocation on instances then it should support it fully (as in case of instance methods) or block user from invoking static method on instances completely. – Tushar Baviskar Jul 21 '14 at 08:48
0

Dynamic Polymorphism can be supported for method overriding. But for static method overriding is not possible because it leads us to method hiding. Hence Dynamic polymorphism is not supported for static methods.

Parveen Kumar
  • 175
  • 3
  • 5
-1

I'm glad I can call a static method through an instance of a class. It lets me call the method from the instance without qualification. And my IDE warns me if I try to (legally but irrationally) call it through an instance.

public static void foo() {
    //yadda yadda
}

public void bar() {
    foo(); // this is legal
    MyClass.foo() // this is also legal, but would be necessary if I couldn't call it through the instance
}

// in some other class:
new MyClass().foo() // this is legal but silly and my IDE warns me about it
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • 1
    But what happens if a superclass declares an instance method called foo? Which method should be called: instance method or static method? I believe the instance method is called in preference to the static method. Meaning another person could alter the superclass and not realise they are effecting the subclass. – Dunes Jul 20 '14 at 22:19