Nested Class

Nested class is a class which is defined inside of another class. To instantiate an nested class you first have to instantiate the outer (a.k.a top level) class. The advantage of having a nested class is, every nested class can access the enclosing (outer or top level) classes variables and methods along with its own methods and variables. Nested classes are used when a class is tightly bound to another class and it does not have any use other than being used with the outer class.

There are four types of nested classes:

  • static member classes
  • non static member classes
  • local classes
  • anonymous classes

Out of these four, you will learn local and anonymous classes. Anonymous classes are heavily used in Android programming.

Anonymous class

Regular classes are first defined and then instantiated with a new keyword. But in Anonymous classes you define the class and instantiate in one step. Anonymous classes are heavily used to implement event listeners in any user interface applications including Android. The reason for this is the event listeners are always bound to the widgets which fire the events and the widgets belong to the top level class. So it makes perfect sense to make event listener a nested class as you never instantiate an event listener without the widgets which are part of top level class. Typically anonymous classes are used when a class needs to implement an interface. The interface is one of the event listener types.

Here is an example:


public class AnonymousClassDemo {

    interface EventListener {
        public void onClick();
    }

    public void clickButton() {
        EventListener listener = new EventListener() {
            public void onClick() {
                System.out.println("you successfully clicked a button!");
            }       
        };

        listener.onClick();

    }
    public static void main(String[] args) {

        AnonymousClassDemo demo = new AnonymousClassDemo();
        demo.clickButton();
    }
 }

In this example, you see an inner interface EventListener declared. The outer class AnonymousClassDemo has a method clickButton which declares an Anonymous class for implementing the EventListener interface. If you did not want to use Anonymous class then the same code would be rewritten as shown below:


public class AnonymousClassDemo {

    interface EventListener {
        public void onClick();
    }

    class EventListenerImpl implements EventListener {
        public void onClick() {
           System.out.println("you successfully clicked a button!");
       }    
     } 

    public void clickButton() {

        EventListenerImpl listenerImpl = new EventListenerImpl();
        listenerImpl.onClick();

    }
    public static void main(String[] args) {

        AnonymousClassDemo demo = new AnonymousClassDemo();
        demo.clickButton();
    }
 }

bulb The name of the class is Anonymous for a reason! Did you notice that in the first implementation you did not see the class name? The class was defined without giving a name for it and the instance of the anonymous class was referenced with the interface variable using the principles of Polymorphism.

In the above example, you not only have an inner interface but also an inner class - EventListenerImpl. EventListenerImpl implements interface EventListener. With an anonymous class, you could avoid defining the class definition and instead define and instantiate in one step.

Lambda Expression

If you anonymous class is too simple with just one method defined, then you can reduce the code clutter by using Lambda Expression. With Lambda Expressions, you can express instances of a single method in a more concise way. Here is the above implementation with a Lambda Expression:


public class AnonymousClassDemo {

    interface EventListener {
        public void onClick();
    }

    public void clickButton() {

        EventListener listener = () -> System.out.println("you successfully clicked a button!");

        listener.onClick();

    }
    public static void main(String[] args) {

        AnonymousClassDemo demo = new AnonymousClassDemo();
        demo.clickButton();
    }
 }

Points to note

  • Lambda expressions are available only from Java 8

  • In android Java 8 is supported from Android Nougat (version 7.0, API level 24)

results matching ""

    No results matching ""