Why does Netbeans suggest making the variable final

So, I downloaded the new version Netbeans 8some time ago. In this new version, every time I declare a variable privateand assign a value to it only once, it netbeansoffers me to make the field final, because it never changes.

Well, I understand that the variable finalhas a value **, which has the advantage of declaring private final int x=3;aka constant over variable private int x=3;and never changes it **

I'm thinking of

  • Concurrency? - But for me it does not make sense if the value of the non-final variable remains unchanged all the time
  • Speed? - I think this may be the reason (some caching?), But I'm not sure

Any clarifications would be appreciated.

+4
source share
1 answer

Netbeans discovers that the variable is actually a constant and invites you to declare it as such. This has the advantage of preventing future errors when this variable accidentally changes.

In addition, the final also indicates the intention to make this variable constant, as well as the usual notation for determining a constant with all uppercase letters, which helps to understand its purpose in the program for other developers supporting the code in the future.

+3
source

Source: https://habr.com/ru/post/1537139/


All Articles