In this post, we will discuss all things final in Java. Final Classes, Final Methods, Final Variables and finally Final Arguments.
Final Classes
Let us look at an example. We have a class A that is declared as final.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This will give a compilation error. The reason is that a final class cannot be subclassed. Once you subclass, you can potentially modify the public methods of the parent class. By declaring the class as final, you as a developer ensure that no unwarranted effects take place with respect to your code.
Some popular classes in the Java library that are declared as final are String, Integer, Boolean.
Final Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now if we make the method in the super class as final
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This gives a compilation error. The final method cannot be overridden.
Final Variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can assign the value to a variable any number of times. However, when you declare the variable as final, it can be assigned the value only once. If you try to assign a value to the variable a second time, it will throw a compilation error.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Although not advisable, you can change the value of the argument in a function. However, when the argument is declared as a final, its value cannot be changed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters