Why are we saying that the static method in Java is not a virtual method?

In an object-oriented paradigm, a virtual or virtual method function is a function or method whose behavior can be redefined within the inheriting class by a function with the same signature to provide polymorphic behavior .


By definition, every non-static method in Java has a virtual method by default, with the exception of finite and private methods . A method that cannot be inherited for polymorphic behavior is not a virtual method.


An abstract class in Java is nothing more than a pure virtual method equivalent to C ++.


Why are we saying that the static method in Java is not a virtual method? Even if we can override the static method and, therefore, it can give some advantages of polymorphism , as well as the static method in Java, you can call it mainly using the class name associated with it, but you can also call it using the object of the associated class in Java with the same way, like the instance method.

+6
source share
5 answers

You cannot override static methods. They are linked at compile time. They are not polymorphic. Even if you try to call it as if it were an instance method (which IMO you should not do), it is bound to the compile time type of this expression, and the run-time value is completely ignored (even if it is zero)

Thread otherThread = null; otherThread.sleep(1000); // No errors, equivalent to Thread.sleep(1000); 

This behavior can be very confusing for the reader, so at least some IDEs allow you to create warnings or errors for accessing static members via a link. It was a flaw in Java design, clean and simple - but it does not make virtual virtual methods at all.

+16
source

It's simple, the static method cannot be overridden by the inheriting class, since it is not inherited. Therefore, it is not virtual.

What you call “static method override” actually defines only another static method for another class. He will only “hide” (and this is actually a much stronger word than what was actually true), different, but does not cancel it.

+3
source

An abstract class in Java is nothing more than a pure virtual method equivalent to C ++.

A class is not a method. An abstract class does not need to have “virtual” or abstract methods or even any methods.

Something C ++ developers put Java functions in the same way as C ++, renamed without understanding the differences .;)

Why are we saying that the static method in Java is not a virtual method?

Not sure who it says, but static methods are not polymorphic.

Even if we can override the static method

We cannot, you can only hide or overload the static method.

If you use a class or subclass or instance to call a static method, the actual class or instance is ignored. for example you can do

 ((Thread) null).yield(); 
+3
source

Since polymorphism is applied to objects, and the static method does not apply to any object (but to a class).

+3
source

Suppose you have class A

 public class A { public static void doAStaticThing() { System.out.println("In class A"); } } 

And B

 public class B extends A { public static void doAStaticThing() { System.out.println("In class B"); } } 

And the method in another class:

 public void foo() { B aB = new B(); bar(B); } public void bar(A anA) { anA.doAStaticThing(); // gives a warning in Eclipse } 

The message you see on the console is

 In class A 

The compiler looked at the declared type anA in the method panel and is statically bound to the implementation of class A doAStaticThing() . This method is not virtual.

+2
source

Source: https://habr.com/ru/post/901300/


All Articles