static and final keyword in java


Static keyword


This is a non-access modifier in java which is mainly used for memory management.


This keyword is applicable to 3 things:



  1. variable

  2. block

  3. methods


Static entities belongs to class.

1. Static variable

This is like a global variable. i.e. shareable to all. And it gets memory once when it is declared inside a class.

2. Static block

The statements of the static block will be executed before the main method.
Don't forget to watch the embedded video

3. Static methods

you can call static methods without creating objects because static belongs to class.


Final keyword



This is also a non-access modifier. This keyword is applicable to:



  1. variable

  2. class

  3. methods


To use final keyword final is used.


1. Final variable



when a variable is declared with the final keyword, its value can not be modified and it is initialized once.


 
2. Final class


when you declare a class as final you can not inherit this class.


 
3. Final Method


when you declare a method as final, you can not override that method.




Comments