Regarding Inner Class

Discussion in 'Java' started by frank, Feb 23, 2009.

  1. frank

    frank Guest

    Can any One tell When we use the Inner classes?what is its use?
    Please Give One Example
    Thanks In Advance
    frank, Feb 23, 2009
    1. Advertisements

  2. blue indigo

    blue indigo Guest

    On Sun, 22 Feb 2009 23:22:29 -0800, frank wrote:

    > 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.)

    blue indigo
    UA Telecom since 1987
    blue indigo, Feb 23, 2009
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kishan bisht
    Jul 2, 2003
  2. E11
    Thomas Weidenfeller
    Oct 12, 2005
  3. Carlo v. Dango
    Alex Martelli
    Oct 19, 2003
  4. Pyenos
    Dec 27, 2006
  5. Replies:
    Joseph Kesselman
    Apr 11, 2007

Share This Page