Final keyword in Java | Variable, Method, Class with examples

Final keyword in java is used to create a Constant Variable, Prevent method overloading and prevent Class Inheritance, etc. Java Final keyword restrict to the user and it can applicable only to: –

  1. Variables
  2. Methods
  3. and Class
Final keyword in Java Variable, Method, Class

Java Final Variable

Defining a variable as a Final make it constant, means you can change (modified) the value of it. You must initialize The Final Variables.

Let’s see the Example of final variable in Java.

There is variable mark has used final keyword. We will try the change the value of it. As per the concept of final the value should not be changed. Once a java final variable value assign then it can’t be changed.

Output: Error:(7, 9) java: cannot assign a value to final variable mark

Nowadays IDE is rich with features, where you will get direct messages about any error and problem. See below screenshot showed an error during programming in IntelliJ IDEA.

Java Final Variable example

Java final method

A main advantage of final method is it can’t override.

Must Read about what is:-

Let’s see the example of how to make the final method and try to override a final method in java.

Output: Error:(10, 10) java: draw() in Rectangle cannot override draw() in ObjectShapes
overridden method is final

IDE itself show error, see below screenshot of code.

Java final method example

Java final class

To prevent your class to be extend use final keyword with class. See below example how to do it. Where ObjectShapes class is final class and trying to extend, but it will show an error.

Output: Error:(7, 25) java: cannot inherit from final ObjectShapes

IDE itself will show error, see below screenshot.

What is Java final parameter (Arguments)?

Answer: A parameter (argument) defined (declared) with the final keyword is called a final parameter. A value of final parameter can’t be changed.

Output: Error:(3, 9) java: final parameter h may not be assigned

What is Java final parameter?

Does final constructor exists in Java?

Or Final keyword with constructor?

Answer: Not its not possible a Java final constructor, because the constructor is never inherited.

Does final constructor exist in Java

Q: Can we use final array in java?

Answer: You can declare Java Final Array without any problem. The final array of elements can change. Where Arrays are objects and object variables (elements of the array) are always references in Java. 

Let’s see one simple example.

Q: What is blank or uninitialized final variable?

Answer: Which Final variables are not initialized at the time of declaration is called a blank final variable. It can be initialized only in the constructor.

Example of blank final variable:-

Let’s initialize blank final variable.

Q: What is static blank final variable?

Answer: A static final blank variable is which not initialized at the time of declaration. It can be initialized only in static block.

Example of static blank final variable:-

Tricky Question: Does final method inherited?

Answer: Yes, you can inherit the final method but can’t override it. See below example:-


You learned about the final keyword means for classes, methods, and variables in Java. You may not use the final keyword often in Java programming, it can be a good design solution.

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java Final keyword examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.