Anonymous inner classes in Java (just read this article)

An anonymous inner class is a local inner class without a name that is typically used to create instances of a class that only need to be used once. Here is some important information about Java anonymous inner classes:

  1. Define anonymous inner class :

    Anonymous inner classes are often used to create objects that implement an interface or inherit a subclass of a class. It has no class name, directly inherits or implements a class or interface, and can create objects in the same line.

    RunnableExample: Create an anonymous inner class object that implements the interface.

    Runnable myRunnable = new Runnable() {
          
          
        public void run() {
          
          
            System.out.println("This is a thread using anonymous inner class.");
        }
    };
    
  2. Characteristics of anonymous inner classes :

    • There is no class name.
    • Only one object can be created because it has no class name and cannot be used again.
    • You can implement abstract methods of interfaces or inherited classes.
    • Can access members of external classes.
  3. Comparison of anonymous inner classes and Lambda expressions :

    In Java 8 and later versions, Lambda expressions were introduced, which provide a more concise way to implement functional interfaces (interfaces with only one abstract method). Lambda expressions are often used in place of anonymous inner classes.

    Example: Use Lambda expression to implement the above Runnable interface.

    Runnable myRunnable = () -> {
          
          
        System.out.println("This is a thread using Lambda expression.");
    };
    
  4. Practical applications of anonymous inner classes :

    • In an event handler, you can use anonymous inner classes to handle events.
    • In collection classes, you can use anonymous inner classes to customize comparators.
    • In multi-threaded programming, you can use anonymous inner classes to implement Runnableinterfaces or Callableinterfaces.
  5. Syntax for anonymous inner classes :

    • Implement interface:

      InterfaceName objectName = new InterfaceName() {
              
              
          // 实现接口方法
      };
      
    • Inherited class:

      ClassName objectName = new ClassName() {
              
              
          // 重写继承的方法
      };
      
  6. Anonymous inner class accesses members of outer class :

    An anonymous inner class can access members of the outer class, but the outer class members need to be declared as final(or actually are effectively final).

    final int x = 10;
    Runnable myRunnable = new Runnable() {
          
          
        public void run() {
          
          
            System.out.println("x is: " + x);
        }
    };
    

Anonymous inner classes are a powerful feature in the Java language. They can simplify the code and make it more readable and compact. Especially in some temporary scenarios, anonymous inner classes can be easily used to implement the corresponding Function.

Guess you like

Origin blog.csdn.net/yang_guang3/article/details/133298411