Inner Class - what is the purpose

E

eyebyte

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
 
P

paul.p.carey

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
 
S

Stefan Schulz

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

Chris Uppal

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
 
R

Raymond DeCampo

Chris said:
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
 
E

eyebyte

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

Roedy Green

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
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top