28

I just discovered local classes in Java:

public final class LocalClassTest
{
  public static void main(final String[] args) {
    for(int i = 10; i > 0; i--) {
      // Local class definition--declaring a new class local to the for-loop
      class DecrementingCounter {
        private int m_countFrom;

        public DecrementingCounter(final int p_countFrom) {
          m_countFrom = p_countFrom;
        }

        public void count() {
          for (int c = m_countFrom; c > 0; c--) {
            System.out.print(c + " ");
          }
          System.out.println();
        }
      }

      // Use the local class
      DecrementingCounter dc = new DecrementingCounter(i);
      dc.count();
    }
  }
}

I did come across this comment: Advantages of Local Classes which listed some great advantages of local classes over anonymous inner classes.

My question is, it doesn't seem like there would be many cases where this technique would have advantages over NON-anonymous inner classes. What I mean is: you would use a local class if your class is only useful inside a method's scope. But when your methods get to the point they are so complex you need to start defining custom classes inside of them, they are probably far too complex already, and need to be split up. At which point, your local class would have to become an inner class, right?

What are some examples of when local classes are more desirable than inner classes, which don't involved super-complex methods (which should be avoided)?

Community
  • 1
  • 1
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129
  • 8
    AAHHHHH... my eyes hurt. :) – limc Jul 14 '10 at 15:21
  • 10
    local class declared in the loop???? are you crazy? You have no need to do that since your class does not access any of loops local variables. This class can even be external easily – Eugene Ryzhikov Jul 14 '10 at 15:23
  • 6
    @eugener: Well, I suppose it should be declared outside the loop, but it's a contrived example which seems equally absurd either way... – Tom Tresansky Jul 14 '10 at 15:26
  • There are a number of times you would want to use local classes. Most are based on object oriented concepts, access modification and information hiding. – Nick Larsen Jul 14 '10 at 15:37
  • if you have to ask this question, especially with the completely inappropriate use in your example, don't do this until you really understand the pitfalls and benefits of this feature, it isn't common for a reason. –  Jul 14 '10 at 15:42
  • 1
    Try using known "inner class" name instead of "local class"... – Betamoo Jul 14 '10 at 16:56
  • 2
    I love the class inside the loop. No chance of accidentially using it elsewhere :) – Thorbjørn Ravn Andersen Jul 14 '10 at 18:02
  • Does this answer your question? [Advantage of Local Classes Java](https://stackoverflow.com/questions/478804/advantage-of-local-classes-java) – Dherik Feb 21 '20 at 19:18
  • 1
    @Dherik - It does help. I already linked it in the question. – Tom Tresansky Feb 21 '20 at 19:25

1 Answers1

13

Local class is something used in some particular method and nowhere else.

Let me provide an example, I used a local class in my JPEG decoder/encoder, when I read configurations from the file which will determine further decoding process. It looked like this:

class DecodeConfig {
    int compId;
    int dcTableId;
    int acTableId;
}

Basically it is just three ints grouped together. I needed an array of configurations, that's why I couldn't use just an anonymous class. If I had been coding in C, I would've used a structure.

I could do this with an inner class, but all the decoding process is handled in a single method and I don't need to use configurations anywhere else. That's why a local class would be sufficient.

This is, of course, the most basic example, but it's from the real life.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • I'm sure you have your reasoning for this approach, but if you are solely shooting for C's structure-alike, wouldn't using Enum be a more proper solution? – limc Jul 14 '10 at 16:17
  • 1
    Enum won't do the trick, I read all the values from the file and they don't form a predefined set of values. Also I need to group three different parameters and I'm not sure how enums could help me do that. – Malcolm Jul 14 '10 at 16:21
  • 2
    Why did you choose to make this "struct" a local class instead of a private inner class? It was only used inside a single method? How complex would you say the method was? – Tom Tresansky Jul 14 '10 at 16:39
  • 3
    Exactly, it isn't used anywhere else. That's the main reason. The method is not complex (under 100 SLoC, by the way), but it has two parts. The first part is reading several configurations and storing them in an array. The second part is using these configurations to decode the following data. This process is done on a very high level, lower level code is hidden inside other classes. I don't need these configurations anymore once I'm done with decoding. Maybe I should edit my answer to explain why I'm using local class in this case, I've only illustrated how I use it (but that fact's implied). – Malcolm Jul 14 '10 at 16:47
  • BTW: since JEP 395 (https://openjdk.java.net/jeps/395) Java provides Records for this, but putting them local is still a good choice, not to pollute namespaces elsewhere. – math Feb 22 '22 at 10:17