What is the advantage of local classes in Java or in any other language that makes use of this feature?
They allow you to take logic out of the parent class and objectify it. This removes functionality from where it doesn't belong and puts it into its own class. But what if this new object is only needed for a short time, only for the duration of a single block of code? Well, that's where a local class fits in.
Here's an example of how an anonymous inner class, a local inner class, and a regular inner class might be present in a program. The example is looking at a myMethod
method and a InnerClass
class present in MyClass
class. For the sake of discussion, those classes will all be implementing the Runnable
interface:
public class MyClass
{
public void myMethod()
{
// Anonymous inner class
Runnable r = new Runnable() {
public void run() {}
};
// Local inner class
class LocalClass implements Runnable
{
public void run() {}
}
}
// ... //
// Inner class
class InnerClass implements Runnable
{
public void run() {}
}
}
The anonymous inner class can be used to simply to make an class that implements Runnable
without actually having to write out the class and naming it, and as krosenvold mentioned in his post, it is used as a "poor man's closure" in Java.
For example, a very very simple way to start a Thread
using an anonymous inner class would be:
new Thread(new Runnable() {
public void run()
{
// do stuff
}
}).start();
An local inner class can be used to make a class that is within the local scope -- it won't be able to be accessed from other methods outside of myMethod
.
If there was another method and we tried to make an instance of the LocalClass
that is located inside the myMethod
method, we won't be able to do so:
public void anotherMethod()
{
// LocalClass is out of scope, so it won't be found,
// therefore can't instantiate.
new Thread(new LocalClass()).start();
}
An inner class is part of the class that the inner class is located in. So, for example, the inner class InnerClass
can be accessed from other classes by MyClass.InnerClass
. Of course, it also means that another method in MyClass
can instantiate an inner class as well.
public void anotherMethod()
{
// InnerClass is part of this MyClass. Perfectly instantiable.
new Thread(new InnerClass()).start();
}
Another thing about the anonymous inner class and local inner class is that it will be able to access final
variables which are declared in the myMethod
:
public void myMethod()
{
// Variable to access from anonymous and local inner classes.
final int myNumber = 42;
// Anonymous inner class
Runnable r = new Runnable() {
public void run()
{
System.out.println(myNumber); // Works
}
};
// Local inner class
class LocalClass implements Runnable
{
public void run()
{
System.out.println(myNumber); // Works
}
}
// ... //
So, what are the advantages? Using anonymous inner classes and local inner classes instead of having a separate full-blown inner class or class will allow the former to have access to final
variables in the method in which they are declared, and at the same time, the classes are local to the method itself, so it can't be accessed from outside classes and other methods within the same class.
There are a number of things that you can do with local classes that you don't get with anonymous inner classes.
- because the type has a name, you can more usefully add members not in the supertype
- a given name can make stack traces easier to follow (particularly for lock objects)
- subtype more than one base type
- construct in more than one place, and multiple constructors
On the other hand they make some hideously verbose syntax even messier.