Java Anonymous Inner Class is an Inner class (non-static nested class) that does not have any name. It is used in java to override the methods of  interface and classes. Unlike other classes, it does not have any  constructor (as the name of a constructor and the name of the class must be the same, but the anonymous inner class doesn’t have a name). It can be created either with an  interface or with a class.

Java Anonymous Inner Class

The anonymous inner class can extend only one class or can implement only one interface, unlike other classes that can extend a class and implement several interfaces simultaneously. There will be only one object created for an anonymous inner class. The expression for an anonymous inner class consists of:

  1. Name of the interface going to be implemented or the class name going to be extended.
  2. The new operator.
  3. Parenthesis for the arguments of the constructor of the enclosing class.
  4. The body containing method declarations.

The anonymous inner class can access the members of the enclosing class. It can also access the local variables in the enclosing block provided that they are declared as final.

#java #java anonymous

Java Anonymous Inner Class Example
2.00 GEEK