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...
The main difference between static and final is that the static is used to define the class member that can be used independently of any object of the class. In contrast, final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited.
The static keyword means the value is the same for every instance of the class. The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.
"private" is an access specifier. It tells you that the member is only visible inside the class - other classes can't access the private members of a class. "static" means that the variable is a class-level variable; there's only one variable, which is shared by all instances of the class.
They are the same. The order of modifiers is not significant. And note that the same rule applies in all contexts where modifiers are used in Java.
It's true that you can not @Override
either method. You can only @Override
a non-final
instance method.
final
, then there's no way you can @Override
itstatic
, then it's not an instance method to begin withIt's NOT true that they're "equal", because one is private static
, and the other is public final
.
static
contextYou 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
).
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With