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

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.

主站蜘蛛池模板: 山丹县| 吉木萨尔县| 洪湖市| 望都县| 建瓯市| 卫辉市| 武冈市| 天柱县| 波密县| 千阳县| 革吉县| 奉新县| 建始县| 武平县| 固安县| 龙里县| 惠来县| 陆河县| 荆门市| 漠河县| 绿春县| 常德市| 天祝| 潼南县| 山西省| 左贡县| 芦溪县| 荔波县| 新乡市| 洛川县| 东阳市| 扶绥县| 大同县| 平和县| 昌宁县| 邻水| 宁陵县| 丽江市| 洱源县| 石河子市| 商都县|