Java – use final for if conditional
•
Java
I know how to set int with final, as follows:
final int junk = whatever ? 1 : 2;
But how do you do this in a more complex if statement?
Solution
How to set a value in an if () condition using java "final"
Example:
final int junk; if(whatever) { junk = 1; } else { junk = 2; }
You can nest the final value settings to any depth, and if you create any duplicates or skip any paths, Java marks errors
Example:
final int junk; if(whatever) { junk = 1; } else { if(however) { junk = 2; } else { junk = 3; } }
I try to use final as a local variable closure to ensure that I don't accidentally reuse variables
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码