* The non-static method belongs to the class instance, can be overridden by the quilt class, thus achieves the polymorphic effect;
Static methods belong to classes and cannot be rewritten, and therefore polymorphic. *
The following is a concrete verification process
First, define a superclass, a, inside a static method and a non-static method:
public class A { public void unstaticMethod() { System.out.println("SuperClass unstaticMethod"); } public static void staticMethod() { System.out.println("SuperClass staticMethod"); }}
Next, define a subclass B, which defines a static method and a non-static method (formally like a method that overrides a superclass):
public class B extends A { public void unstaticMethod() { System.out.println("SunClass unstaticMethod"); } public static void staticMethod() { System.out.println("SunClass staticMethod"); }}
In the next step, we test:
public class Test { @SuppressWarnings("static-access") public static void main(String[] args) { A a = new A(); A a2 = new B(); a.unstaticMethod(); a2.unstaticMethod(); a.staticMethod(); a2.staticMethod(); }}
Operation Result:
SuperClass unstaticMethodSunClass unstaticMethodSuperClass staticMethodSuperClass staticMethod
From the running result, we can know that for the non-static method, the effect of polymorphism is realized, but the static method is not.
Can a static method in Java be overridden?