Inner Class - what is the purpose

Discussion in 'Java' started by eyebyte@yahoo.com, Jun 23, 2005.

  1. Guest

    I am just curious what is the purpose of the Java inner class? It
    seems it is mainly used for listener or for GUI development

    For other who have been developing java application, could you give me
    an example when or how you could use the innerclass. I am trying to
    justify the purpose of it.

    Thanks for your thoughts, samples and ideas.

    - Joe
     
    , Jun 23, 2005
    #1
    1. Advertising

  2. Guest

    Some classes don't make make much sense on their own - they only make
    sense in the context of another class. Inner classes are helpful at
    defining this kind of relationship - they allow the developer to
    express the relation between highly cohesive classes more explicitly.

    As an example consider a Node in a Graph. A Node has a list of peer
    Nodes which it can reach itself. It makes sense to define a Peer class
    as an inner class of Node. For example:

    public class Node
    {
    private String name;
    private List peers = new ArrayList();

    private Node( String name )
    {
    this.name = name;
    }

    /**
    * Represents a peer node that is reachable from this node
    */
    class Peer
    {
    private Node node;
    private int distance;
    }
    }

    Other examples can be seen by browing the Javadocs or source, for
    example HashMap and HashMap.Entry.

    Paul

    wrote:
    > I am just curious what is the purpose of the Java inner class? It
    > seems it is mainly used for listener or for GUI development
    >
    > For other who have been developing java application, could you give me
    > an example when or how you could use the innerclass. I am trying to
    > justify the purpose of it.
    >
    > Thanks for your thoughts, samples and ideas.
    >
    > - Joe
     
    , Jun 23, 2005
    #2
    1. Advertising

  3. Harald Guest

    Harald, Jun 23, 2005
    #3
  4. On Thu, 23 Jun 2005 21:39:14 +0000, Harald wrote:

    > writes:
    >
    >> I am just curious what is the purpose of the Java inner class? It
    >> seems it is mainly used for listener or for GUI development

    >
    > Not at all. See Roedy's very nice glossary:
    >
    > http://mindprod.com/jgloss/innerclasses.html


    While i do not agree to everything written in that entry (or in the
    entire glossary, for that matter), there is something i wish to add

    Inner classes (even anonymous ones) are very useful for "slim interfaces"
    such as Comparator, Runnable and similar. Implementing them in the main
    class would screw that classes semantics, but hiding the fact that we do
    use a Runnable somewhere, or need a Comparator for something (which we
    might no longer do in the next revision) solves the problem while keeping
    the parents interface clean.

    I, however, strongly advocate named inner classes... and would discourage
    more then one top-level class in the same file. Now that confuses me.
    Anonymous classes, however, look like an ugly hack to me... but i do admit
    to using them on occasion... (though i usually refactor them to something
    nicer pretty soon)

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jun 24, 2005
    #4
  5. Chris Uppal Guest

    wrote:

    > For other who have been developing java application, could you give me
    > an example when or how you could use the innerclass. I am trying to
    > justify the purpose of it.


    It may help to think of inner classes as /just/ syntactic sugar. That is to
    say, they provide /nothing/ that cannot be expressed (pretty directly) in Java
    without using them. All they do is provide a somewhat more lightweight way of
    defining objects for some limited/local purpose that might seem not to justify
    the apparent weight of a "full" standalone class. Of course, the appearance is
    deceptive, but if it help make the code clearer then that's a good thing even
    if -- when all's said and done -- it's just an illusion.

    -- chris
     
    Chris Uppal, Jun 24, 2005
    #5
  6. Chris Uppal wrote:
    > wrote:
    >
    >
    >>For other who have been developing java application, could you give me
    >>an example when or how you could use the innerclass. I am trying to
    >>justify the purpose of it.

    >
    >
    > It may help to think of inner classes as /just/ syntactic sugar. That is to
    > say, they provide /nothing/ that cannot be expressed (pretty directly) in Java
    > without using them. All they do is provide a somewhat more lightweight way of
    > defining objects for some limited/local purpose that might seem not to justify
    > the apparent weight of a "full" standalone class. Of course, the appearance is
    > deceptive, but if it help make the code clearer then that's a good thing even
    > if -- when all's said and done -- it's just an illusion.
    >


    This is especially true when you realize that the Java byte code
    interpreter does not support inner classes. The Java compiler is
    responsible for converting inner classes into "ordinary" classes by
    adding the necessary access methods and/or instance variables.

    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Jun 24, 2005
    #6
  7. Guest

    Thanks to all who responded. It clearer to me now the purpose of the
    inner class. I thought it was just for listerner or gui development but
    it is more than that.

    Never thought about the slim interface that Stefan mentioned and I
    liked that synthetic analogy.

    Everyone responded gives me a better picture. It makes sense now...
     
    , Jun 24, 2005
    #7
  8. Roedy Green Guest

    On 23 Jun 2005 13:04:37 -0700, wrote or quoted :

    >I am just curious what is the purpose of the Java inner class? It
    >seems it is mainly used for listener or for GUI development
    >
    >For other who have been developing java application, could you give me
    >an example when or how you could use the innerclass. I am trying to
    >justify the purpose of it.


    see http://mindprod.com/jgloss/innerclasses.html
    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jun 24, 2005
    #8
    1. Advertising

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. E11
    Replies:
    1
    Views:
    4,800
    Thomas Weidenfeller
    Oct 12, 2005
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,046
    Alex Martelli
    Oct 19, 2003
  3. Pyenos
    Replies:
    2
    Views:
    392
    Pyenos
    Dec 27, 2006
  4. Replies:
    8
    Views:
    414
    Greg Herlihy
    Jun 28, 2008
  5. Phlip
    Replies:
    2
    Views:
    453
    phlip
    Jul 1, 2008
Loading...

Share This Page