What is the advantage of local classes in Java or in any other language that makes use of this feature?
The local class is in the scope of the enclosing scope, and has the same access to names outside the function as does the enclosing function. Declarations in a local class can use only type names, static variables, extern variables and functions, and enumerators from the enclosing scope.
Local classes are similar to inner classes because they cannot define or declare any static members. Local classes in static methods, such as the class PhoneNumber , which is defined in the static method validatePhoneNumber , can only refer to static members of the enclosing class.
The main advantages of a nested (inner) class are: It shows a special type of relationship, in other words, it has the ability to access all the data members (data members and methods) of the main class including private. They provide easier code because it logically groups classes in only one place.
An inner class is just a class inside a class. A local class is an inner class declared inside of a block.
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.
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