Theory:Final variables
# Theory:Final variables
Sometimes, you need to use a variable that should not be modified during the program. Such variables are known as constants. Java provides a special keyword called final
to declare them. The only difference between a regular variable and a final variable is that we cannot modify the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of the program.
# Final varibles
注意
Note, that the compiler will produce an error when trying to modify the value of a final variable.
Important, if a final variable has not been assigned before using it, the compiler also will produce an error.
提示
A final variable can be reassigned to a regular variable without any restrictions. The value of a regular variable can be changed any time as always.
# Final reference variables
The final
keyword can be legally used with reference variables. In this case, the final keyword means that it is not possible to reassign a reference to the variable.
Here is an example with the StringBuilder
class which is a mutable version of String
.
final StringBuilder builder = new StringBuilder();
builder = new StringBuilder(); // error line
2
In this code, the second line won't compile since we are trying to reassign a reference to the final variable builder
. But there is one important point.
注意
Note, that it is always possible to change the internal state of an object pointed at by a final reference variable, i.e. the constant is only the variable itself (the reference), not the object to which it refers.
So, the following code is absolutely correct:
final StringBuilder builder = new StringBuilder(); // ""
builder.append("Hello!"); // it works
System.out.println(builder.toString()); // Hello!
2
3
As you can see, this code changed the internal state of an object (""
→ "Hello!"
) referenced by a final variable. When we invoked append()
method we changed not the object itself but just the value of its fields. append()
method is one of the main operations on a StringBuilder
that are not available in String. It converts its argument to a String
and then appends its characters to the character sequence.
The final keyword makes a constant reference, but does not prohibit changing the internal state of the object.
Since Java 11, it is also possible to use final
with var
to use the automatic type inference for the constant variable.
final var FINAL_VAR = 10; // int
final var MSG = "Hello!"; // String
2
# When to use final keyword
Some programmers mark all variables that they do not want to modify as final
. In this case, the program will contain a lot of such variables.
final Scanner scanner = new Scanner(System.in);
final int a = scanner.nextInt();
final int b = scanner.nextInt();
System.out.println(a + b);
2
3
4
This approach allows you to write programs with the minimum number of mutable variables which usually leads to fewer errors. In addition, the Java compiler can optimize code with final variables effectively and your program can be a little faster. But this is not always predictable behavior and needs some advanced knowledge.
There is also a contra-argument: massive use of the final
keyword makes your code less readable (boilerplate code (opens new window)).
During your real work as a programmer, we hope that the issue of using finals will be standardized for all programmers in the project.
提示
Interestingly, the final
keyword can be also used in some different contexts, not only for declaring constants.