Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

oop - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...