0

As far as I see, final is commonly used comparing to the same situations in C#. There is no final in C#, but sealed is not heavily used like this. So, can we say that; if the value will not be changed, or at least we are sure, then we should use final for all the situations e.g. variable definitions, parameters, ...?

private final Integer LIMIT = 10;
private final String NAME = "John";

or

default Optional<EmployeeDTO> findByUuid(final UUID uuid) {
  ...
}

And should we use capital letters for the final variable names as constant values?

  • 2
    Use it if you think it makes your code clearer or might prevent an error. You don't need to use it everywhere possible. That would be cumbersome. – khelwood May 10 '21 at 20:56
  • 2
    Note in Java the `ALL_CAPS` convention is typically reserved for fields that are both `static` _and_ `final` (that includes enum constants). Fields that are just `final` use the standard `camelCase` convention. – Slaw May 10 '21 at 20:58
  • @khelwood In this scene, I think it only helps to prevent from further value assign and there is no difference for performance, etc. Is that true? –  May 10 '21 at 20:59
  • 1
    In the example you gave, `LIMIT` and `NAME` are actual constants (there's no means to set them to anything else), so you ought to make then `static final`. As for other cases... it's preference. I personally like when they're final, but adding the modifier clutters the code, so I usually don't. – chrylis -cautiouslyoptimistic- May 10 '21 at 21:00
  • And, what about the constant values in Java? Is the only difference `final static` to use constant values? **AND** should I use **capital** letters for `final` variables as `constant` values in Java? –  May 10 '21 at 21:00
  • 1
    `final` fields _do_ have significant performance advantages, particularly in multithreaded code, since they have guarantees that they will never change. `final` method parameters don't. _Do not_ use `SNAKE_CASE` for `final` fields; use `camelCase` like for variables. – chrylis -cautiouslyoptimistic- May 10 '21 at 21:01
  • 1
    @Slaw Thanks Slaw, I have just seen your reply. Then I will use camelCase for final and CAPITAL for `static final` meaning `constant` in Java. Is that true? –  May 10 '21 at 21:02
  • @chrylis-cautiouslyoptimistic- Good explanations, thanks. –  May 10 '21 at 21:03
  • I think *effectively final* variables have the same performance advantages. – khelwood May 10 '21 at 21:10
  • @khelwood Sorry, do not understand what you mean. Do you explain a little bit further? Do you agree with **chrylis-cautiouslyoptimistic-** for performance wise? –  May 10 '21 at 21:15
  • "effectively final" means a local variable that the compiler can tell does not ever get changed, so it can treat it as final even if it isn't declared final. – khelwood May 10 '21 at 21:31
  • Then, can we say that we **should** use `final` for global values, but there is no difference when using `final` for local values and method parameters? –  May 10 '21 at 22:19
  • @khelwood Any reply please? –  May 11 '21 at 06:52
  • You already got my opinion in my first comment. – khelwood May 11 '21 at 06:59
  • @khelwood Thanks, but I am confused with all of comments. So, could you please that **Then, can we say that we should use final for global values, but there is no difference when using final for local values and method parameters?** –  May 11 '21 at 08:20

0 Answers0