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

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.

主站蜘蛛池模板: 赞皇县| 汪清县| 修文县| 库尔勒市| 青海省| 赤城县| 农安县| 通山县| 台南县| 鹤山市| 龙海市| 呼图壁县| 凤凰县| 灵山县| 东城区| 兴安盟| 南雄市| 青浦区| 自贡市| 凌源市| 安乡县| 平凉市| 阜康市| 扶风县| 江门市| 保定市| 罗定市| 大名县| 当雄县| 封开县| 梧州市| 永城市| 西丰县| 延寿县| 西城区| 门头沟区| 大田县| 青河县| 房产| 临海市| 建始县|