2

I ran over an interesting question yesterday.

The current definition for a Block Statement is here.

BlockStatement: LocalVariableDeclarationStatement ClassDeclaration Statement

This means that you can do something like this

class A {

    public void doIt() {
        class B {}
        B b = new B();
    }
}

or this

public class Test {
    static 
    {
        class C {}
        C c = new C();
    }
}

Good to know that this actually works, but can anybody think of a reasonable use case where you would actually want to do something like this? Where it probably would be the most elegant solution compared to something else?

Community
  • 1
  • 1
emboss
  • 38,880
  • 7
  • 101
  • 108
  • As I answered in that other question, it can be used as an alternative to an anonymous inner class, if you need to define a constructor in the class (which isn't possible in an anonymous inner class). – Jesper Jul 10 '11 at 16:21
  • That's definitely an advantage, but still I would want to prefer an inner class of the enclosing class for that, no? – emboss Jul 10 '11 at 16:31
  • Why? Making a local class out of it limits the scope, it's often a good idea to make the scope of variables and classes as small as possible. – Jesper Jul 10 '11 at 18:05

2 Answers2

2

I imagine you'd do this when extending or implementing a class or interface, where the implementation is very specific to the scope that you're in. Typically you see people doing this with anonymous inner classes, but if you want to get a little more code reuse, while still restricting the class to the scope of the method you're in, you could use one of these local inner classes instead.

Update

In many cases, you're going to find that a private static class makes more sense than a local inner class. However, standard practice dictates that we restrict the scope of a class to the narrowest scope that works, to avoid cluttering namespaces and such.

Also, Java uses local classes to implement/imitate closures. Converting CloseWindowListener in the code below into a private static class would require a lot more code, because you'd have to create a custom constructor and private field to contain the window variable. But because the class is defined within the scope of this method, we can access it directly instead.

public void setupWindowHandlers(Window window) {
    class CloseWindowListener extends ActionListener {
        public void actionPerformed(ActionEvent event) {
            window.close();
        }
    }
    closeButton.addActionListener( new CloseWindowListener() );
    cancelButton.addActionListener( new CloseWindowListener() );
}
Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • I see, it's more code reuse than using anonymous inner classes (a lot of boilerplate if you do this multiple times in the same method) - but instead of scaring your co-workers with such a construct wouldn't it make more sense to use a private (static) inner class of the enclosing class :) ? – emboss Jul 10 '11 at 16:03
  • Nice argument. It beats using two anonymous inner classes because of the boilerplate that is saved. – emboss Jul 10 '11 at 22:48
2

From Effective Java 2nd Edition:

If you need to create instances from only one location and there is no preexisting type that characterizes the class, then make it a local class.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94