anonymous inner classes in java

anonymous inner classes in java

Anonymous inner classes in Java are a type of inner class that does not have a name. They are declared and instantiated in a single expression, typically as part of a method call or instantiation of an interface. Anonymous inner classes are useful for implementing interfaces or extending classes with minimal syntax. Here’s a detailed explanation:

anonymous inner classes in java

1. Definition:

  • Anonymous inner classes are inner classes that do not have a name.
  • They are defined and instantiated in a single expression, typically as an argument to a method call or instantiation of an interface.

2. Syntax:

  • Anonymous inner classes are declared using the `new` keyword followed by the class or interface being extended or implemented, along with the class body enclosed within curly braces `{}`.
  • Since they do not have a name, they cannot have constructors.

3. Usage:

  • Anonymous inner classes are commonly used for providing implementations of interfaces with a single method (functional interfaces) or extending classes with minimal syntax.
  • They are often used in event handling, GUI programming, and callback mechanisms, where a short-lived implementation is needed.

4. Access to Outer Class Members:

  • Anonymous inner classes have access to all members (fields, methods, and parameters) of the enclosing class, similar to other types of inner classes.
  • They can also access final local variables of the enclosing method.

5. Instance Relationship:

  • Instances of anonymous inner classes are associated with instances of the enclosing class, similar to other types of inner classes.
  • However, since they are typically short-lived and declared inline, their lifecycle is limited to the scope in which they are declared.

6. Visibility and Scope:

  • Anonymous inner classes are scoped to the method or expression in which they are declared.
  • They cannot be accessed from outside the method or expression and are typically used for short-lived implementations.

Example
```java
public class AnonymousInnerClassExample {
    public void sayHello() {
        // Declare and instantiate an anonymous inner class
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello from anonymous inner class!");
            }
        };

        // Execute the run method of the anonymous inner class
        runnable.run();
    }

    public static void main(String[] args) {
        // Instantiate the outer class and call the method
        AnonymousInnerClassExample outerObj = new AnonymousInnerClassExample();
        outerObj.sayHello();
    }
}
```

In this example, an anonymous inner class implementing the `Runnable` interface is declared and instantiated inline within the `sayHello()` method. The `run()` method of the `Runnable` interface is implemented with a single-line print statement.

Summary:

Anonymous inner classes in Java are inner classes without a name, declared and instantiated in a single expression. They are useful for implementing interfaces with minimal syntax and extending classes inline. Anonymous inner classes have access to the members of the enclosing class and are scoped to the method or expression in which they are declared. They are commonly used for short-lived implementations and callback mechanisms.