views:

135

answers:

2

I read that doing:

public final void foo() {}

is equals to:

private static void foo() {}

both meaning that the method is not overridable!

But I don't see the equivalence if a method is private it's automatically not accessible...

+15  A: 

It's true that you can not @Override either method. You can only @Override a non-final instance method.

  • If it's final, then there's no way you can @Override it
  • If it's static, then it's not an instance method to begin with

It's NOT true that they're "equal", because one is private static, and the other is public final.

  • They have different accessibility level
  • The instance method requires an instance to invoked upon, the class method doesn't
  • The class method can not refer to instance methods/fields from the static context

You can not @Override a static method, but you can hide it with another static method. A static method, of course, does not permit dynamic dispatch (which is what is accomplished by an @Override).

References

Related questions

polygenelubricants
Also, the public final method can override some superclass method, while the private static, being static, can not.
ILMTitan
+2  A: 

Neither can be overridden, but for very different reasons. The first is a public nonstatic method, while the secod is static. So the first is not overridable only because it has been declared final, while the second, being static, can never be overridden.

Note that from the first you can access nonstatic members of the class, while from second you can't. So they are used in very different ways, thus are not "equal".

Péter Török