aspose file tools*
The moose likes Beginning Java and the fly likes Why use inner classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why use inner classes?" Watch "Why use inner classes?" New topic
Author

Why use inner classes?

sudhakar
Greenhorn

Joined: Sep 26, 2006
Posts: 2
what is the use for inner classes

Changed to a more descriptive subject line
[ September 26, 2006: Message edited by: Merrill Higginson ]

sudhakar
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
sudhakar,

We're glad you dropped by the Java Ranch. There aren't many rules here, but we do ask that you let us know who you are by giving us your actual first and last names. Please make sure the "publicly displayed name" in your profile meets the JavaRanch naming policy.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Also..this is not a Struts question but a general Java question.

- Brent
Nitin Gupte
Greenhorn

Joined: Sep 12, 2006
Posts: 6
One important use of Inner classes is when you have to implement same method with different functionalities. Like you have two buttons which do two different things. On the click event of each button you have to give two different implementaions for actionPerformed(ActionEvent event) method, in such a case you can use two inner classes that implement the ActionListener interface and pass their reference to addActionListener(ActionListener al) method.
I'm sure there can be other uses of Inner classes as well.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I guess there are several reasons to use the various flavors of inner classes.

Using an anonymous inner class instead of a regular class avoids making up a name, making and building a separate source file.

You can enforce certain design concepts, such as an instance of an inner class that can only exist in the scope of an instance of the outer class. Frankly I'd have to go read a book to figure out how to do this. It hasn't ever been important to me.

You can restrict access to the inner class. Nobody else can do a "new" on an anonymous inner class, only the enclosing class can create it. If the enclosing class does not hand out references, it could be that nobody else even knows the inner class exists.

An inner class can access private data of the enclosing class in ways a totally separate class could not.

Any of those spark more questions?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi Sudhakar

Inner Classes were introduced in Java1.1
and are used for specific purposes.

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields



# Nested classes that are not inner classes may declare static members.
# Since Interfaces are always implicitly static so they are never considered to be inner classes.


Below are some other variants of Class

Nested Class (Static Inner Class)
Local Class
Anonymous Class

[ September 28, 2006: Message edited by: Pratibha Malhotra ]

~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791

Below are some other variants of Class

Nested Class (Static Inner Class)
Local Class
Anonymous Class


Good clarification. I probably ran all those together. Study each one to avoid the kind of confusion I have.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why use inner classes?