Why did Java's designers consider it useful/necessary?
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.
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.
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).
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.
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.
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:
Test
to do the background processing, orBoth 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With