6

Possible Duplicate:
Java final modifier

This is just a small question about preferences. What would be the correct usage for the final modifier?

I have noticed that going from source-to-source that I tend to use it more than others. I put it for the parameters for a method, for variables in the method and what ever I can. Is that necessary or am I just overdoing it?

Reference:

private final int x;
private final int y;
private final int id;
private final int width;
private final int height;

public Widget(final int id, final int x, final int y, final int width, final int height) {
    this.id = id;
    this.x = x;
    this.y = y;
    this.xmod = x;
    this.ymod = y;
    this.width = width;
    this.height = height;
}
Community
  • 1
  • 1
  • 4
    [Java final modifier](http://stackoverflow.com/questions/4012167/java-final-modifier) – Smit Feb 01 '13 at 17:52
  • Can you share some examples? – Koray Tugay Feb 01 '13 at 17:52
  • I think you should use `final` only when it is necessary. Like anything, you shouldn't abuse it. – Jeel Shah Feb 01 '13 at 17:58
  • The smart thing would have had the language default to immutable but alas they opted for language user convenience. The final keyword achieves nothing of real value. When was the last time you had an encapsulated function break as a result of reassignment? Probably never. It's just overly pedantic and ritualistic. I use it on instance variables where it has real impact given the shared nature of the variable across functions. – Rick O'Shea Sep 20 '22 at 19:23

5 Answers5

6

Yes, you are overdoing it. Historically, this might have resulted in some optimizations -- e.g. final methods can be inlined more easily -- but these days, most of those optimizations are done whether or not you actually label the method final.

The places you should still use final are on most fields in classes, on classes that shouldn't be, or aren't, extended, on methods you don't want overridden, and on local variables that need to get referenced in anonymous inner classes.

In particular, it is overkill to do it on local variables and on method parameters that will not get wrapped in anonymous inner classes.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 2
    +1 I agree that overuse contributes to clutter, especially on method parameters and local variables. Also `final` in general has no runtime implications http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html – Steve Kuo Feb 01 '13 at 18:08
  • Would the downvoter care to explain? – Louis Wasserman Feb 01 '13 at 19:05
  • 1
    Someone probably likes final clutter. I know people who put final on all variables. – Steve Kuo Feb 01 '13 at 19:32
  • The availability of final just exposes a flawed design because immutability should be the default. That does not mean you the programmer needs to be burdened with fixing the flaw and supplying the "final" qualifier yourself. By all means use it with instance variables but it's practically useless in block scope. – Rick O'Shea Sep 20 '22 at 19:26
2

I would say that any field, class or method that will never be changed or overridden should always be final. This makes it thread safe and allows the compiler and vm to make optimisations. It also clarifies your intent as a programmer and tells other developers that the value never changes so if they are looking for complexity they can look elsewhere.

Good article here: http://www.javapractices.com/topic/TopicAction.do?Id=23

span
  • 5,405
  • 9
  • 57
  • 115
  • I would say you are being overly ritualistic and straining to fix a broken language decision on your own, every time you write code. They default to mutable and you should go with that and you will find that using "final" on block scoped variables is a pointless academic exercise that will only clutter up your code. Use it on instance variables by all means. – Rick O'Shea Sep 20 '22 at 19:29
1

I would say that you should use "final" whenever you don't want a variable to be changed after it was initialized or if you don't want a class do be subclassed or a method to be overridden but subclasses. Regarding compiler optimization, I thought that the usage of "final" keyword would help in some way, but I just read this:

A common misconception is that declaring a class or method final improves efficiency by allowing the compiler to directly insert the method inline wherever it is called. In fact the compiler is unable to do this because the method is loaded at runtime and might not be the same version as the one that was just compiled. Only the runtime environment and JIT compiler have the information about exactly which classes have been loaded, and are able to make better decisions about when to inline, whether or not the method is final.

http://en.wikipedia.org/wiki/Final_(Java)

Pedro Rodrigues
  • 424
  • 5
  • 16
1

If Java is designed today, all local variables and method parameters would be final by default.

Unfortunately, since the language doesn't do that, should we do that manually instead?

It just seems too much chore. If we don't do it, and there doesn't seems to be a huge problem, then we can be lazy and don't do it.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • 100% CORRECT. Defaulting to mutable variables was the wrong decision. Use final for instance variables. Use the default in block scope because that's what the Java language designers intended. I would not obsess over mutable block scoped vars. – Rick O'Shea Sep 20 '22 at 19:33
0

It is not necessary to make variables final everytime. It may become a headache when you want to change the value of such variables.

I think , you should decalre a variable final only if it is univarsely constant, or when you think it should not be changed.

Rakesh Burbure
  • 1,045
  • 12
  • 27