Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Inner Class - what is the purpose

Reply
Thread Tools

Inner Class - what is the purpose

 
 
eyebyte@yahoo.com
Guest
Posts: n/a
 
      06-23-2005
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

 
Reply With Quote
 
 
 
 
paul.p.carey@gmail.com
Guest
Posts: n/a
 
      06-23-2005
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

http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Harald
Guest
Posts: n/a
 
      06-23-2005
(E-Mail Removed) 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

Harald.

--
---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
 
Reply With Quote
 
Stefan Schulz
Guest
Posts: n/a
 
      06-23-2005
On Thu, 23 Jun 2005 21:39:14 +0000, Harald wrote:

> (E-Mail Removed) 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"


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) 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



 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      06-24-2005
Chris Uppal wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
eyebyte@yahoo.com
Guest
Posts: n/a
 
      06-24-2005
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...

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-24-2005
On 23 Jun 2005 13:04:37 -0700, (E-Mail Removed) 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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: c++ class design: where to put debug purpose utility class? Phlip C++ 2 07-01-2008 02:49 AM
class design: where to put debug purpose utility class? 1230987za@gmail.com C++ 8 06-28-2008 05:14 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments