-4
  1. For what version of Java is it planned to implement the possibility to override static methods?

  2. For what year it would be?

  3. How to achieve similar thing in Java 8?

  4. Is there some hack for Oracle JVM?

  5. Are there some other virtual machines or compilers, able to override static methods?

I see that there is confusion about word "static" in java. What I am really looking for, is the methods of the class, (it is irrelevant, if you name them "static method", "class method" or anything else..)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jii
  • 7
  • 1
  • 4
  • Static method cannot be overridden because it's not associated with any instance of a class. Period. – Yahya Jul 11 '17 at 12:41
  • 1
    You can't override static methods. You are not going to be able to override static methods. – khelwood Jul 11 '17 at 12:41
  • @Yahya In theory, it is not true, because "class methods" can be overriden. And in practice, it is not true either, because there are many languages, what enable it. Question is inly, when oracle will enable it in java.... – Jii Jul 12 '17 at 04:25
  • @khelwood Sorry, I really do not understand, what do you like to help? Are you a oracle employer with future plan knowing? – Jii Jul 12 '17 at 04:26
  • Future features are discussed using the JCP. It is **not** about some people at Oracle secretly making such decisions. It is a *open community process*. – GhostCat Jul 12 '17 at 13:53
  • Possible duplicate of [Why doesn't Java allow overriding of static methods?](https://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods) – howlger Jul 31 '17 at 22:29

1 Answers1

1

That is the whole point why many people consider static to be an abnormality for good OOP: it kills polymorphism (besides introducing tight coupling between your classes).

Thus the simply answer is: overriding and static do not go together. Not with Java versions before 8 or 9; and most likely: also never in the future - I am not aware of any plans / projects / JCPs to change that.

In that sense, the only answer is: simply avoid using it.

If at all, you put it on small helper methods that are "self-contained" (meaning they do some simple functional thing that works fine in your unit-test environment). As soon as static gets into your way of easily unit-testing code, you are going down the wrong road.

When you find static in legacy code you have to deal with - then consider defining an interface for the underlying functionality so that you can decouple your code from the actual static methods.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thank you for useful text. But mainly I am looking for answer to my original question(s). So, OK, it is important to have good design of the software. But in case, there exist "class methods", there is no reason to force programmer to only one approach to "class methods inheritance" (as not to allow inherit them). – Jii Jul 12 '17 at 04:39
  • 1
    Well. That is simply how things are in Java. Arguing about a design decision made almost 20 years ago isn't too productive in my eyes. – GhostCat Jul 12 '17 at 05:01
  • Generics arrived. When will this type of inheritence follow? – Jii Jul 12 '17 at 13:39
  • Generics fixed a big problem. And even that was done in a "super soft" way to **not** break backwards compatibility (from a JVM perspective). And many years later, we still don't have "real" generics (that work for primitive types, or would be "reifable"). Beyond that: genercis didn't fall from the sky. There were several projects upfront to figure how to do it; and zillions of discussions took place. And as said: I am not aware of any JCP that would ask for this. Seriously: I am working with Java for 20 years, I know people who once worked on javac ... – GhostCat Jul 12 '17 at 13:51
  • In other words: I have *some* insight into the Java language world. Guess what: I think you are the **first** person I ever met who asked to enhance Java to allow for polymorphic static calls. Please let that sink in. It doesn't make sense to argue here. If you want this feature, you go forward and put up a JCP entry; and convince people that this is required. Good luck with that! – GhostCat Jul 12 '17 at 13:52