Final Keyword in Java

The final keyword in Java is a very important keyword that is used to apply some restrictions to the user. The final keyword can be applied to class, method, and variables in Java. In this article, we will see What is the final variable in java, the final method in java, and final class in java. It is the most common and important question asks in the interview. An interviewer can ask what is the use of final keyword in java and final keyword in java with example? What does it mean by making the final variable, final method, and final class in java? Why we use it?  

Final is a reserved word or keyword in Java that can be used with a variable to make a constant, used with a method to restrict the method overriding, used with a class to restrict the inheritance of class.

final keyword in java

1. final variable in java

In java, final variables are just like constants. If you want to make any constant variable, then use the final keyword with the variable. You can’t change the value of a final variable once it is initialized. So, we should initialize it.
Note: It is not mandatory to initialize a final variable at the time of declaration. You can initialize it by constructor you can read it in detail. If you are not initializing it at the time of declaration, it’s called a blank final variable.
It is good practice to represent final variables in all uppercase, using an underscore to separate words. You can read it in detail here.

Here is the table content of the article we will cover this topic.
1. What is the final variable in Java?
2. How to declare a final variable?
3. Initialization of the final variable?
i) Initialization while declaration?
ii) Initialization with the constructor?
iii) Initialization with a static block?

4. Re-assign value to a final variable?
5. Local Java final variable?
6. final with a reference variable?
7. final with an array variable?

8. What is the blank final variable in java?
9. RULES to initialize the blank final variable in the constructor?
10. Real-life example?
11. Why we use the blank final variable?

12. static final in java?
13. final reference variable?

dataType final variableName;
int final a = 0;
String final s = "Hi";
float final = 0.2f;

2. final method in java

We can declare a final method by use of the final keyword. A final method can’t be overridden in child class. If we want to enforce the implementation to all the derived class then we can use the final method. By use of the final method, we can apply the restriction in inheritance. You can read it in detail here.

Here is the table content of the article we will cover this topic.
1. What is the final method in Java?
2. How to use the final method?

3. Important points about the final method?
4. When to use the final method?

accessModifier final returnType methodName()
  // Body of method
public final void data()
   // Body of method

3. final class in java

We can declare a class as a final class with the final keyword. If a class is a final class, then it can’t be extended(inherited). By use of final with classes is to create an immutable class like the predefined String class. You can read it in detail here.

Here is the table content of the article we will cover this topic.
1. What is the final class in Java?
2. How to create a final class?
3. Advantages of the final class?
4. When to use a final class?

final class className
    // Body of class
final class Student
   // Body of class

Can we declare a constructor with the final keyword?

No, we can’t declare a constructor with the final keyword. As you know, the final method can’t be overridden in a driven class. Because the final keywords prevent a method from being modified in a subclass. So it is basically used for sensitive information or security purpose. The objective to make a method final is that the data/content of the final method must not be able to change by anyone.

As you already know about inheritance, You can inherit the members/methods of a base class in the derived class. But you can’t inherit the constructors of the base class. So there is no need to make a constructor as a final.

final class in java

4 thoughts on “Final Keyword in Java”

  1. Sir, your way of explations Java is very good. You have performed all parts of Java very well. I have read all these Java parts very well and I have learned a lot from it.


Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.