1
class A{

    public static void staticMethod(){
     System.out.println("Static method of A");
    }
}

class B extends A{

    public static void staticMethod(){
     System.out.println("Static method of B");
    }
}

class TestStaticOverride{

    public static void main(String args[]){
     B b=new B();
     A a=b;
     a.staticMethod();
    }
}

The output is "Static Method of A" . So static methods are not overridden otherwise the output would have been "Static Method of B". How at runtime JVM decides to call the static method of class A and not B.

falsarella
  • 12,217
  • 9
  • 69
  • 115
Pankaj
  • 14,638
  • 3
  • 17
  • 23
  • 6
    Have a look here: http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods – Fido488 Aug 06 '14 at 00:46

5 Answers5

2

Edit: When you call a static method, whether through an instance or at the class-level, the compiler will recognize it as static and therefore treat it at the class-level, not object-level. This means that the method will have no knowledge of the state of the object that the method was invoked from. If using an object to call the method, such as in your example, the compiler will therefore only be able to know which method to call, A or B, based on what that object was declared as. If it was instantiated as a different subclass it won't matter, because it is a static method, and it has no notion of objects. So, in your example, this is why it will call A's static method rather than B's.

Hopefully this was clearer explanation that what I had before

radar
  • 595
  • 2
  • 11
  • Not so much. Maybe you can explain it better with code? – ChiefTwoPencils Aug 06 '14 at 00:51
  • A static method is not associated with any instance of a class, it is a property of the class itself. Although `a.staticMethod()` will work, it is recommended to call `A.staticMethod()` instead. – cyc115 Aug 06 '14 at 00:57
  • 1
    As I have read more on the subject I have discovered that: "Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it." Since both `a` and `b` are declared of type `A` then it is evident that the static method of A will be invoked. – cyc115 Aug 06 '14 at 01:19
  • 1
    `b` is being treated as an instance of `A` because you assigned it to a variable declared as `A a`. This is an implicit typecast, where you are treating the child class as the parent, so the static method refers to the one on the parent class. If instead you did `B b=new B(); B a=b; a.staticMethod()`, then the static method will change to the one from class `B`, I believe. – ps2goat Aug 06 '14 at 05:19
2

Static methods are resolved at compile time and that's why they are bound to a class where they are defined, not to an instance. So they belong to a class, not to an instance and that's why another name is a "class method". You can call a static method from an instance according to the Java syntax, but it will be always considered as you calling it from a class, e.g.

A a;
a = [no matter what];
a.static_method();

is absolutely the same as:

A.static_method()

regardless of what value you assign to 'a'.

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
1

Although it is possible, it is not recommended to call a static method from an instance.

A a = new B();
a.staticMethod();

A good practive is calling it directly from where it is:

A.staticMethod();

Note: I think you can configure that warn messages at your IDE.


As you can see in the StackOverflow's tag:

Static is a term used in some programming languages to define a function or data storage area (field) that is not bound to any specific object instance.

falsarella
  • 12,217
  • 9
  • 69
  • 115
1

it is bad practice because JVM won't understand the instructions you are trying to give, moreover imagine what a mess your OO would be?

for instance:

class A{

public static void met(){
System.out.println("Class A");
}
}

class B extends A{

//Override
public static void met(){
System.out.println("Class B");
}

}

let's make a Polymorphism:

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

a.meth();//Class A
b.meth();//Class A !!! <===(Was Expected to be Class B)
Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
-1

You cannot override static method. If you remove static in staticMethod(), when you call a.staticMethod(), then staticMethod() in B class will be called.

class A {
public void staticMethod() {
    System.out.println("Static method of A");
}
}

class B extends A {
    public void staticMethod() {
        System.out.println("Static method of B");
    }
}

class Main {
    public static void main(String args[]) {
        B b = new B();
        A a = b;
        a.staticMethod();
    }
}

// Result: Static method of B

In other word, if you try to override static method, then method from super class will be called.