1

I have a bunch of static final methods within a final class:

public class final Util{

    private Util{}

    // do I need the keyword final when the class is already final
    public static final util1{..}
    public static final util2{..}
    public static final util3{..}

}

I know, instance methods are implicitly final when its class is final. However, is this the case also for static methods?

nimo23
  • 5,170
  • 10
  • 46
  • 75

1 Answers1

3

Final classes cannot be extended in the first place, so it does not matter whether or not their methods are marked as final.

For example, this will fail:

final class Util {
}
class Extra extends Util {
}

error: cannot inherit from final Util
class Extra extends Util {

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Static methods (without final) can be overriden (can be hidden).There is a difference of making static methods final or not final – nimo23 Feb 06 '21 at 15:23
  • @nimo23 but you cannot extend the class, so there is nowhere to "hide" them from. You are never able to generate a conflict, because final classes cannot have subclasses. – knittl Feb 06 '21 at 15:24