What does “final” do if you place it before a variable?

Short Answer

Stops the “myTextField” variable being assigned to something else.

Long Answer

  • Does NOT stop the “myTextField” variable being mutated, e.g. having its fields set to new values.
  • Makes code more readable (IMHO) because the reader never has to wonder whether the “myTextField” variable will be reassigned later on in the code.
  • Guards against the category of bug whereby variables are accidentally reassigned (same reasoning behind making instances immutable, only on a smaller scale).

For the reasons given above, I always apply the “final” modifier wherever I can to static fields, instance fields, local variables, and method parameters. It does bloat the code a little, but for me it’s worth the extra readability and robustness.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)