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:
Eclipse Save Actions

Related posts:

  1. Yet Another Reason final is Your Friend
  2. My Newest Insight into the Generics Controversy
  3. Extending Java Syntax
  4. Functional Code Win of the Day
This entry was posted in To Be Categorized and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • http://virtual-void.de Johannes Rudolph

    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.

  • http://www.linkedin.com/in/robertfischer Robert Fischer

    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.

  • http://www.linkedin.com/in/robertfischer Robert Fischer

    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.

  • http://ludoa.wordpress.com LudoA

    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

  • Bruno De Fraine

    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.

  • http://www.linkedin.com/in/robertfischer Robert Fischer

    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, final gave 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.

  • http://ludoa.wordpress.com LudoA

    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.

  • Pingback: Enfranchised Mind » Some final Patterns

  • Pingback: Upped The Recent Post/Popular Post Widget Count | Enfranchised Mind

  • http://catchpole.net/ Christian Catchpole

    Those save actions look awesome and it’s something I’d like to apply to a large code base. I don’t use Eclipse, but I certainly will if I could process my code base using this. But will I have to open every file, (touch it), and then save it? I can’t find any re-factoring tools out there that will do this as a batch.

    • http://www.smokejumperit.com Robert Fischer

      Might want to look at the (now-abandoned?) Jalopy: http://jalopy.sourceforge.net/

      There’s probably other source code formatting tools out there (or IDE macros?) that could do this for you.

      Otherwise, just apply it as you touch files: it’ll probably result in some source code changes anyway if you’re working on a legacy codebase, so you may not want to do it programmaticly.

  • Categories