elibraryportal Logo

Final Keyword in Java | Example

final keyword is keyword this is non-access modifier.

Use of Final Keyword

There are there way use that is ..

  1. Final Variable
  2. Final Method
  3. Final Class

1) Final Variable in Java

→ When it is initialized, we can not change the value of final variable so it is constant.

Example:-

Output:- Compile Time Error

Fianl Variable Example in java

Example2:-

Output:-

Fianl Variable Example in java

2) Final Method in Java

→ If you want to make any method as final, you can not override .

Example:-

3) Final Class in Java

→ If a class is declare by a final keyword, it cannot be extended(inherited), because show Compile Time Error. that is called Final Class.

Example1:-

Output

Compile Time Error
Fianl class Error

Example2:-

Output

Fianl class
Next Topic