Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why use inner classes?

 
sudhakar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use for inner classes

Changed to a more descriptive subject line
[ September 26, 2006: Message edited by: Merrill Higginson ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also..this is not a Struts question but a general Java question.

- Brent
 
Nitin Gupte
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic