what is anonymous class in java

1 year ago 69
Nature

An anonymous class in Java is an inner class without a name. It is a way to declare and instantiate a class at the same time, making code more concise. Anonymous classes are like local classes, except that they do not have a name. They are usually used to extend subclasses or implement interfaces. Anonymous classes must be defined inside another class, and their syntax is similar to the invocation of a constructor, except that there is a class definition contained in a block of code. Anonymous classes can be useful when making an object with certain "extras" such as overloading methods of a class or interface, without having to actually subclass a class. Fields, extra methods, instance initializers, and local classes can be declared in anonymous classes.