What is final in Java? Final variable, Method and Class Example


Java Final Keyword 

• A java variable can be declared using the keyword final. Then the final variable can be assigned only once. 
• A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it. 
• Java classes declared as final cannot be extended. Restricting inheritance! 
• Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not. 
• final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding. 
• Java local classes can only reference local variables and parameters that are declared as final. 
• A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance. 

Final variables
A final variable can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases. [4] (Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.)
Unlike the value of a constant, the value of a final variable is not necessarily known at compile time. 
Example:


1. /*
2. Java Final variable example
3. This Java Example shows how to declare and use final variable in
4. a java class.
5. */
6.
7. public class FinalVariableExample {
8.
9. public static void main(String[] args) {
10.
11. /*
12. * Final variables can be declared using final keyword.
13. * Once created and initialized, its value can not be changed.
14. */
15. final int hoursInDay=24;
16.
17. //This statement will not compile. Value can't be changed.
18. //hoursInDay=12;
19.
20. System.out.println("Hours in 5 days = " + hoursInDay * 5);
21.
22. }
23. }
24.
25. /*
26. Output would be
27. Hours in 5 days = 120
28. */

You can see here :- Java Final Method with Example

People who read this post also read :



2 comments:

final is a tricky keyword if used carefully it can boost performance of Java application. see here for detailed explanation of final keyword in Java

A clear and useful explanation of Java’s final keyword, especially how it helps maintain immutability, prevent method overriding, and restrict class inheritance. The practical example of a final variable makes the concept easy to understand and highlights why using final appropriately can improve code reliability and maintainability.
Sporting Equipment
real-time communication for tradies

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More