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 classes, methods, 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 asked 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 do 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, and used with a class to restrict the inheritance of class.
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. After initialization of the value, we can’t the value of the final variable.
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?
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. What is the static final variable in java?
13. How to declare a static final variable?
14. Initialization of static final variable?
15. What is the static blank final variable in java?
16. RULES to initialize the static blank final variable?
17. What is the 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 the child class. If we want to enforce the implementation to all the derived classes 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.