Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

final and private static

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...

like image 572
xdevel2000 Avatar asked Jun 16 '10 08:06

xdevel2000


People also ask

What are the differences between private static and final variables?

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.

What does final and static mean?

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.

What does it mean to be private and static?

"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.

What is difference between static final and final static in Java?

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.


2 Answers

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

  • JLS 8.4. Method Declarations
    • 8.4.3.2 static Methods
    • 8.4.3.3 final Methods
    • 8.4.8 Inheritance, Overriding, and Hiding
      • 8.4.8.1 Overriding (by Instance Methods)
      • 8.4.8.2 Hiding (by Class Methods)

Related questions

  • Polymorphism vs Overriding vs Overloading
  • Why doesn’t Java allow overriding of static methods ?
  • Static methods and their overriding
  • When do you use Java’s @Override annotation and why?
like image 111
polygenelubricants Avatar answered Oct 03 '22 19:10

polygenelubricants


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".

like image 36
Péter Török Avatar answered Oct 03 '22 20:10

Péter Török