File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes When to use inner classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When to use inner classes?" Watch "When to use inner classes?" New topic

When to use inner classes?

Shaik Muhammad
Ranch Hand

Joined: Jul 16, 2008
Posts: 36
Can you anyone please tell me when should i go for inner classes? How does it help if i use an inner class replacing a separate class?
Sanjeev Dubey

Joined: Aug 07, 2008
Posts: 16
hi Shaik,

There are lots of discussion done on the SCJP side of Java ranch..
you can follow it up there...
its quite interesting and with lots of real time examples.

hope that helps

Shaik Muhammad
Ranch Hand

Joined: Jul 16, 2008
Posts: 36
Thanks sanjeev
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Its common to use an inner class when adding an ActionEvent to a listener, for example in Swing. This removes the need to conditionally test what button has been clicked in a function to designate the appropriate action: A quick sketch example would be....


The inner class would then extend the relevant abstract classes for action-Events and the overridden method ActionPerformed() will handle the button click. Take a look also on the Swing Forums here at the ranch.

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
I agree. Here's the link:
subject: When to use inner classes?
It's not a secret anymore!