Difference Between Static and Final in Java

JavaServer Side ProgrammingProgramming

In this post, we will understand the difference between ‘static’ and ‘final’ keywords in Java.

Static

  • It can be applied to nested static class, variables, methods and block.

  • It is not required to initialize the static variable when it is declared.

  • This variable can be re-initialized.

  • It can access the static members of the class only.

  • It can be called only by other static methods.

  • Objects of static class can’t be created.

  • Static class can only contain static members.

  • It is used to initialize the static variables.

Final

  • It is a keyword.

  • It is used to apply restrictions on classes, methods and variables.

  • It can’t be inherited.

  • It can’t be overridden.

  • Final methods can’t be inherited by any class.

  • It is needed to initialize the final variable when it is being declared.

  • Its value, once declared, can’t be changed or re-initialized.

raja
Published on 24-Mar-2021 17:39:21
Advertisements