I’m not getting inner classes. What’s the reason for using inner classes in Java? Can you give me a good example of static and non static nested classes? What are the advantages / disadvantages of it?
Cheers
I’m not getting inner classes. What’s the reason for using inner classes in Java?
Share
Anonymous inner classes are useful for defining one-shot classes with very few methods, for instance when implementing an event listener or a comparator. In these cases, a full-blown class would be overkill, given that it’s not going to be used anywhere else.
Here’s a simple example of a comparator for a string list implemented as an anonymous inner class:
Or if
myArrayListhappens to be a generic list, this is how it would look like: