Dec 05 2007
Eclipse and the Automagical Final
As anyone who has seen my code knows, I’m a big fan of using the final keyword in Java (see Object Burn is Your Friend and Yet Another Reason Final Is Your Friend).
One thing that always annoyed me is that Eclipse has very poor support for final — it’s hard to change their code generation templates to include final, because only some of them are directly accessible through the application.
But, at some point, that changed out from under me. In their Java editor save actions, you can have it append final to all kinds of stuff. See this thumbnail for more:
Popularity: 6% [?]








Don’t overrate the importance of final. At least for local variables it is only a syntactic feature which is not even reflected in the compiled code.
I’m not overrating it — I’m just a big fan. That “syntactic feature” is nice enough in its own right. Being able to explicitly say “Hey, this reference shouldn’t change. If you want it to change, you’re probably breaking something.”, and being forced to declare at the point when you have enough information to instantiate (and not before) is useful enough for me.
Plus, it will often catch typos or brainfarts where I substitute in one variable (which is supposed to change) in for another.
The way I figure it, if 1) I can get it for free and 2) it catches bugs at compile-time, then I’m all for it.
Hi,
I just read your posts about final, but I have to admit I don’t really get it.
What’s the advantage of this:
public void setFoo(final int foo) { this.foo = foo; }
over this:
public void setFoo(final int foo) { this.foo = foo; }
And what’s the advantage of this:
for(int i = 0; i < 10; i++) {
String msg = “We are at position ” + i;
System.out.println(msg);
msg = null;
}
over this:
String msg = null;
for(int i = 0; i < 10; i++) {
msg = “We are at position ” + i;
System.out.println(msg);
}
msg = null;
Or am I totally misunderstanding what code-style you’re advocating?
I’m very interested in your opinion.
Thanks,
Ludo
Ludo, if it’s not referenced in the article, you could read the chapter The ‘final’ story from the book “Hardcore Java” for a good explanation of all the virtues of final.
Yeah, I reference that chapter in one of my posts. The one thing to note is that the book is a bit out of date. Once upon a time,
finalgave some performance boost, but those are pretty much gone at this point — the Java compiler has gotten pretty good about realizing when you’re dealing with immutable data.Ludo — Two things: 1) your first pair of contrasting code examples are identical to each other; 2) I will respond to the spirit of your post, but I’ve been a bit busy. It might also end up as its own blog post.
Thanks a lot, Bruno & Robert - I’ll read the final story.
Robert: you’re right, I meant “public void setFoo(int foo) { this.foo = foo; }”, so without the ‘final’.
Looking forward to your post about this - but take your time :) Enfranchised Mind is in my RSS reader now, so I won’t miss it.
[...] As promised, here are a few useful cases for final in Java. In reality, there are so many of them that I’ve just gotten into the habit of using it unless I find a good reason not to: it just seems like the habit of using final makes ugly and buggy code more difficult to write, and since we are all our own coding horror, we need all the help we can get! [...]
[...] Eclipse and the Automagical Final [...]