官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 珲春市| 黄大仙区| 阿克陶县| 新平| 普兰县| 吉首市| 乌拉特前旗| 怀远县| 兴海县| 福贡县| 含山县| 成武县| 商河县| 沽源县| 随州市| 咸阳市| 外汇| 海伦市| 洪泽县| 西平县| 德惠市| 墨玉县| 班玛县| 贵定县| 石泉县| 疏附县| 资阳市| 灵山县| 黎川县| 威海市| 玉环县| 嘉鱼县| 灵璧县| 乌什县| 楚雄市| 南乐县| 福清市| 高尔夫| 盐津县| 中牟县| 渭南市|