Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of Local Classes Java

What is the advantage of local classes in Java or in any other language that makes use of this feature?

like image 289
Paradius Avatar asked Jan 26 '09 03:01

Paradius


People also ask

Why is local class useful?

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.

What is the use of local class in Java?

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.

What are the advantages of inner classes in Java?

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.

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

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.

like image 126
coobird Avatar answered Oct 06 '22 15:10

coobird