10.5 Anonymous Inner Classes
Anonymous classes go one step further than local classes: These inner classes have no names and always automatically create an object; class declaration and object creation are combined into one language construct. The general notation is as follows:
new ClassOrInterface() { /* Members of the inner class */ }
New methods and object variables can be declared, and methods can be overridden in the block of curly brackets. new is followed by the name of a class or interface, with the following options:
-
new Class name ( Optional Arguments ) { ... }: If new is followed by a class type, then the anonymous class is a subclass of Class name . Possible arguments for the constructor of the base class can be specified ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access