Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Inner Class - what is the purpose

Thread view: 
eyebyte@yahoo.com - 23 Jun 2005 21:04 GMT
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
paul.p.carey@gmail.com - 23 Jun 2005 22:27 GMT
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

> 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
[quoted text clipped - 6 lines]
>
> - Joe
Harald - 23 Jun 2005 22:39 GMT
> 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

 Harald.

Signature

---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software

Stefan Schulz - 24 Jun 2005 00:16 GMT
>> 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)

Signature

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"
         

Chris Uppal - 24 Jun 2005 09:58 GMT
> 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
Raymond DeCampo - 24 Jun 2005 13:02 GMT
>>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
[quoted text clipped - 7 lines]
> 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

Signature

XML is the programmer's duct tape.

eyebyte@yahoo.com - 24 Jun 2005 15:53 GMT
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...
Roedy Green - 24 Jun 2005 19:02 GMT
>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
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.