Regarding Inner Class

F

frank

Can any One tell When we use the Inner classes?what is its use?
Please Give One Example
Thanks In Advance
 
B

blue indigo

Can any One tell When we use the Inner classes?what is its use?
Please Give One Example

We're having a five for one special today:

1. Anonymously implement things like ActionListener, Runnable, and the
like, in a method body, approximating having first-class functions.
2. Keep things like ActionListeners, SwingWorkers, and the like internal
to a class instead of breaking encapsulation. For example, a Swing UI
class may contain an inner ActionListener instead of implementing
that interface itself and having to expose a public actionPerformed()
method that will do something bad if someone calls it from outside at a
random time. (It's a shame Sun doesn't seem to know about this use, to
judge by javax.swing.JCheckBox!)
3. Implement things like iterators, sublists, and the like within the
collection class they belong to, where many may need to exist at once
and all are backed by the associated parent collection.
4. Enum constants with behavior (methods specific to the constant) are
implemented as inner (or at least nested) classes of the enum.
5. Implementation classes that are not exposed can be completely hidden
and private, and can also have access to private members of a visible
class, by making them inner classes. LinkedList has an inner Node class
that is not exposed to its users (as well as an inner Iterator class
that is; see item number three). Trees are another case likely to
contain Nodes. This has some overlap with items 2 and 1, keeping
internal ActionListeners and the like internal, with the difference
being that in the one case you need first-class function like behavior
which in Java is had by implementing an interface, which requires a
new class, and in the other (and item 3) you need multiple instances
of the inner objects, which requires a new class, and possibly access
to the enclosing object's private state. (If you don't need such
access, you can also use an external, private class within the same
source file, or an external, default-access class within the same
package, though.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top