Inheriting static methods in Java

So I know that in Java when you have a static method you are supposed to call it with the format ClassName.method() rather than use the same structure as you would for instance methods, namely:

ClassName myObject = new ClassName();
myObject.method();

However, if you were to do it this way it would still be valid code and would work. Let's say I decide to do this where the method in question is static, and have the following setup:

public SuperClass {
    public static int foo(int x) {
        return x;
    }
}

public SubClass extends SuperClass {
    public static int foo(int x) { // Overriding foo() in SuperClass
        return x + 1;
    }
}

public MyDriver {
    public static void main(String[] args) {
        SuperClass myObject = new SubClass(); // Upcasting.
        System.out.println(myObject.foo(5));  // This should polymorphically print 6
    }
}

What prints out on the screen, however, is 5 rather than 6. Why?

4 Answers
  1. Static methods apply only to the class they are defined in, and they cannot be overridden.

    When you call myObject.foo(5), you are calling SuperClass.foo(5) in reality, because you declared myObject as a SuperClass, regardless of whether you instantiated it as one.

    The proper way to call a static method is to call it directly from the class it is declared in, so if you wanted to call SubClass.foo(), you must call it from an explicitly declared SubClass instance (meaning no upcasting), or you need to call SubClass.foo() like so.

    The simple answer is that calling static methods from instances evaluates to calling those same methods from the declared type with no instance rather than the instance type.

    I am not certain of this, but I would not be surprised if when the code is compiled into byte-code, that the instance static method call would actually be compiled into a direct call to the declared type.

    Edit: An upvote brought my attention back to this and I cleaned up the explanation to make it more clear and fix some negligible grammatical mistakes on my part. I hope this helps future readers.

    2014-05-20 03:11:18
  2. Using instances of a class to call that class's static methods is something you should avoid, since it can cause confusion. If you need to call any method polymorphically, make it an instance method. You cannot polymorphically call a static method. The reason the SuperClass invocation is called is because that is the apparent class of myObject at compile-time. This effect can also be seen in the following scenario:

    public void doSomething(SuperClass param) {
        System.out.println("SuperClass");
    }
    
    public void doSomething(SubClass param) {
        System.out.println("SubClass");
    }
    
    public void test() {
        SuperClass myObject = new SubClass();
        doSomething(myObject);
    }
    

    If test() is called, SuperClass will be printed.

    Martin Wickham2013-08-04 03:43:45
  3. Static methods don't depends on the instance, they belong to the class and only to the class, in fact if you have a static method you'll always access to one unique instance always.

       myObject.foo(5) 
    

    is only a shortcut, what you really are doing is

       SuperClass.foo(5)
    
    2013-08-04 03:38:46
  4. Static methods are treated as global by the JVM, they are not bound to an object instance at all. By the way, you can only overload static methods, but you can not override. So check for "Oracle Documentation for Overriding and Hiding Documents".

    Defining a Method with the Same Signature as a Superclass's Method:

    -----------------------------------------Superclass Instance MethodSuperclass Static Method   
    Subclass Instance Method      Overrides                                     Generates a compile-time error
    Subclass Static Method            Generates a compile-time error   Hides                                         

    kalibrain2013-08-04 05:12:08
Related Articles
You Might Also Like