Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of a method-local inner class?

Tags:

Why did Java's designers consider it useful/necessary?

like image 523
andandandand Avatar asked Jul 26 '09 00:07

andandandand


People also ask

What is the use of local inner class?

Accessing Members: A local inner class has access to fields of the class enclosing it as well as the fields of the block that it is defined within. These classes, however, can access the variables or parameters of the block that encloses it only if they are declared as final or are effectively final.

What is local inner class?

A class i.e., created inside a method, is called local inner class in java. Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop, or an if clause. Local Inner classes are not a member of any enclosing classes.

What is true about method-local inner class?

It can be marked static. Explanation: Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful).

What local variables can a local inner class use?

Since the object of an inner class exists within an object of its outer class. Therefore, a local inner class can access the instance variables of its outer class as well as local variables which are in scope.


2 Answers

Since most people probably have never seen a method-local inner class, here is an example:

public class TestMethodLocalInnerClass
{
    public static void main(String[] args)
    {
        class Greeter implements Runnable
        {
            private final String _greeted;

            public Greeter(String greeted)
            {
                super();
                _greeted = greeted;
            }

            public void run()
            {
                System.out.printf("Hello %s!\n", _greeted);
            }
        }

        new Greeter("world").run();
        new Greeter("dog").run();
    }
}

This could theoretically be useful as an additional level of encapsulation below an inner class when an anonymous class can't be used because you need more than one instance. Maybe if you need say different comparators in different methods and need to instantiate them more than once. This seems to be very very rare (I never wrote such a class before), and it is not a big deal to use normal inner classes instead.

So in my view it would not be worth to include it in a newly designed language.

like image 143
starblue Avatar answered Sep 29 '22 10:09

starblue


Using method-local classes can increase the readability of your code by keeping related parts together. As a contrived example, suppose you have a method that needs to create a thread to do something in the background:

class Test {
    void f() {
        // Method local inner class
        class InnerClass {
            private String myThreadName;
            // InnerClass constructor
            public InnerClass(String myThreadName) {
                this.myThreadName = myThreadName;
            }
            // InnerClass method
            public void run() {  
                Thread thread = new Thread(
                    // Anonymous inner class inside method local inner class
                    new Runnable() {
                        public void run() {
                            doSomethingBackgroundish();
                        }
                    }
                );
                thread.setName(myThreadName);
                thread.start();
            }
        }
        InnerClass anInnerClass = new InnerClass(aThreadName);
        anInnerClass.run();
    }
}

Without method-local classes, you would have to either:

  • create a new named class inside Test to do the background processing, or
  • create a new named class in a separate source file to do the background processing.

Both these options can reduce the readability of the code by moving the related processing somewhere else in your source tree (maybe in the same source file, maybe in another source file entirely). Using a method-local class keeps the processing logic right where it is used.

This is certainly not applicable for all situations, but it can be very useful in a number of common situations. Such code is commonly used for GUI action listeners, which are very small classes that usually just relay action method calls from one place to another.

like image 30
Greg Hewgill Avatar answered Sep 29 '22 10:09

Greg Hewgill