0

My question is basically the same as this one, but does this also apply to static functions?

I want to understand:

  1. Does the compiler treat all the static functions in a final class as final?
  2. Does adding final keyword to static functions in a final class have any effect?
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
stonar96
  • 1,359
  • 2
  • 11
  • 39
  • 1
    Any non-final methods declared in a final class, whether those methods are static or not, are implicitly final insofar as there can never be a subclass in which any potentially hiding or overriding method can be declared. However, the non-final methods of a final class _do not_ have the final modifier added; in other words, you could query the modifiers of such methods via reflection and they _would not_ be final. This is different behavior than, for instance, interfaces where method declarations can omit the public modifier and yet those methods will still literally be public. – Slaw Sep 12 '19 at 05:32
  • @Slaw thanks for your detailed comment. Actually this should be the accepted answer. – stonar96 Sep 12 '19 at 06:36
  • 1
    Does this answer your question? [Behaviour of final static method](https://stackoverflow.com/questions/1743715/behaviour-of-final-static-method) – Arvind Kumar Avinash Feb 06 '21 at 15:14
  • @LiveandLetLive thanks for linking the question. However, I know in principle what `final`, `static`, overriding of methods and hiding of static functions mean. The question was more like: "In the case of a static function, is there anything else that the `final` keyword does, except to prevent hiding it?". The answer seems to be "No.". However, if the answer was "Yes.", then it could actually make a difference to make a static function `final` in an already `final` class. – stonar96 Feb 06 '21 at 18:06

2 Answers2

5

In a final class, all methods are implicitly final as well because making the class final means it cannot be inherited from and thus there can be no hiding or overriding of its methods in child classes.

As a result, effectively you are correct that a static method in a final class is final, but it's not because of the fact that it's a static method. It's because the class is final.

jwenting
  • 5,505
  • 2
  • 25
  • 30
1

All static functions are final so those in a final class are as well.

EDIT: As you pointed out, declaring a static method final will prevent subclasses from hiding it by declaring an identically named method. If a class is final, then this property does not matter however since there could not exist a subclass to hide the static method. In this sense, yes all static methods are implicitly final.

kingkupps
  • 3,284
  • 2
  • 16
  • 28
  • A static function cannot be overridden, but it can be hidden. The final modifier prevents it from being hidden by a sub-class static function. That means that the final modifier makes a difference, so not all static functions are final. – stonar96 Sep 12 '19 at 04:06
  • 2
    @stonar96 Does it really matter? If a class is final there can never be a subclass and thus there is no possibility of any method being overridden/hidden. – Slaw Sep 12 '19 at 04:30
  • @Slaw I agree it does not matter for the name hiding/overriding case. – kingkupps Sep 12 '19 at 04:31
  • @Slaw yes, in the case of final classes. Not in the case of non-final classes containing static methods. So it's the final on the class that makes the method final, not the static on the method. – jwenting Sep 12 '19 at 04:43
  • @jwenting I don't believe that counters my point. If a class is final then it is _impossible for any static method to be hidden by a method in a subclass_, thus making whether or not the final modifier is implicitly added irrelevant. At least in "normal" usage; as StephenC mentions in the linked Q&A, there is a somewhat noticeable effect for things such as reflection. – Slaw Sep 12 '19 at 04:57
  • @Slaw wasn't meant to contradict you, but to clarify that the finality of the method isn't caused by the application of static to it. – jwenting Sep 12 '19 at 04:58
  • @Slaw _Does it really matter? ..._ That's what the question is about. I know that the final modifier for classes prevents subclassing and thus there is no possibility of any method being overridden/hidden. But it could be that I did not take something else into account, where it actually matters, when a final modifier is added to a final function in a final class. In my first comment I just wanted to clarify that at least a part of the answer was wrong. kingkupps due to your edit I have undone my downvote and I have upvoted. Thanks to all. – stonar96 Sep 12 '19 at 06:31