Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use a local class in Java?

Tags:

java

oop

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)?

like image 744
Tom Tresansky Avatar asked Jul 14 '10 15:07

Tom Tresansky


People also ask

What is the use of local inner class in Java?

Method-local Inner Class In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined.

What does Local do in Java?

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.

What is a local class?

A class declared inside a function becomes local to that function and is called Local Class in C++. A local class name can only be used locally i.e., inside the function and not outside it. The methods of a local class must be defined inside it only. A local class can have static functions but, not static data members.

How does a local class differ from an inner class?

An inner class is just a class inside a class. A local class is an inner class declared inside of a block.


1 Answers

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.

like image 111
Malcolm Avatar answered Sep 30 '22 13:09

Malcolm