nospam said:
What are the exact benefits of inner classes?
In one sense, they have no benefits. That's to say, there's nothing you can do
with inner, nested, or anonymous classes that you can't do -- and do quite
easily -- with "normal" top-level classes. Indeed the whole inner-class thing
is just syntactic sugar for top-level classes.
The benefits are more about what you /can't/ do with them. You can't "see"
them except as part of their containing class. Or (in the case of anonymous
classes) see them at all. Well, you /can/ see them, but you won't do so by
accident. So, as bugbear, has already said, there's a benefit of
encapsulation, and there's also a benefit in reducing the overall clutter of
top-level names.
Also (even though it's mostly an illusion) creating a little inner or nested
class for some specific (but very limited) purpose doesn't /feel/ as much of a
heavy-weight operation as creating a top-level class, so programmers are more
likely to use little classes. Bugbear also mentioned they are good for use in
callbacks; that's true, and the reason they are good for that is that creating
a top-level class for each callback would /feel/ like a complete pain (and
thinking of names for them would /be/ a pain ;-).
-- chris