• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why use inner classes?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also..this is not a Struts question but a general Java question.

- Brent
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic