Final variables

In our case, tmp is a final local variable. The scope of this variable is limited to the block following the if statement, and inside this block, this variable gets a value only once. The block is executed many times during the code execution, and each time the variable gets into scope, it gets a value. However, this value cannot be changed in the block. This may be a bit confusing. You can think about it as having a new tmp each time the block executes. The variable gets declared and has an undefined value and can get a value only once.

Final local variables do not need to get the value where they are declared. You can assign a value to a final variable some time later. It is important that there should not be a code execution that assigns a value to a final variable that was already assigned a value before. The compiler checks it and does not compile the code if there is a possibility of the reassignment of a final variable.

To declare a variable to be final is generally to ease readability of the code. When you see a variable in a code declared to be final, you can assume that the value of the variable will not change and the meaning of the variable will always be the same wherever it was used in the method. It will also help you avoid some bugs when you try to modify some final variables and the IDE will immediately complain about it. In such situations, it is likely to be a programming mistake that is discovered extremely early.

In principle, it is possible to write a program where all variables are final. It is generally a good practice to declare all final variables that can be declared to be final and, in case some variable may not be declared final, then try to find some way of coding the method a bit differently.

If you need to introduce a new variable to do that, it probably means you were using one variable to store two different things. These things are of the same type and stored in the same variable at different times but, logically, they still are different things. Do not try to optimize the use of variables. Never use a variable because you already have a variable of the type in your code that is available. If it is logically a different thing, then declare a new variable.
While coding, always prefer source code clarity and readability. In Java, especially, the Just In Time compiler will optimize all this for you.

Although we do not explicitly tend to use the final keyword on the argument list of a method, it is good practice to make sure that your methods compile and work if the arguments are declared final. Some experts, including me, believe that the method parameters should have been made final by default in the language. This is something that will not happen in any version of Java, so long as Java follows the backward compatibility philosophy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.229