aspose file tools*
The moose likes Beginning Java and the fly likes about InnerClasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about InnerClasses " Watch "about InnerClasses " New topic
Author

about InnerClasses

saravana kumar
Ranch Hand

Joined: Jun 25, 2002
Posts: 72
what is the use of inner classes ? why do have an inner classes in java and where is need to use that inner classes in java ?

thanks in advance
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

There are a variety of intended advantages that inner classes offer:
- Showing "tight" relationships between two classes by defining one within another.
- Keeping 'trivial' objects separate from 'important' ones by keeping them nameless. Anonymous inner classes are frequently used to describe an object that is detailed enough to be separated from a "higher-level" class but dependent enough on the surrounding code that it is best left nameless.
- Providing a style of code-writing that seems cumbersome or harder to maintain with normal package scoping.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Inner classes have many uses in Java. One common use is implementation of a factory pattern. The host (enclosing) class is declared to be abstract and you define one or more private or proteced inner classes that extend the enclosing class. You then have a static method(s) that will return an instance of one of those inner classes. Inner classes are also used in instances where the inner class is very tightly coupled to the host class and there is little chance of being able to reuse the inner class in some other situation. Anonymous inner classes are used in a similar manner but are usually used in cases that call for the implementation of a simple interface and can be constructed easily on the fly. Static inner classes just create a quick and dirty package extension. It is important to remember that inner classes have complete access to all the state of their host classes. Of course that does not apply to static inner classes.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
ME have you take over JY's job of posting just seconds before me?
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
I don't code professionally *yet*, but I find anonymous inner classes VERY friendly...when I have a button or something that is very specific, I can just define right there what I want to have happen (say in an anonymous ActionEvent or mouseClick) and then move on. I don't like having to write an entire separate class just to have a frame exit or something simple like that. Again, I am not a professional and there may be dis-advantages to this style (please let me know if this is so!!), but I sure like it for now. I save creating whole new classes for when I have multiple things going on all over the place...then the code for events (or whatever) is centralized.


David Crossett
-nothing important to say, but learnin' plenty-
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Michael Morris:
ME have you take over JY's job of posting just seconds before me?

Am I doing it wrong?
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by David Crossett:
I don't like having to write an entire separate class just to have a frame exit or something simple like that. Again, I am not a professional and there may be dis-advantages to this style (please let me know if this is so!!), but I sure like it for now.

Anonymous inner classing can be a real drag to read, maintain and debug. That's the big drawback. It also opens the door to a few perverse, if amusing, code styles that I won't go into. Don't want to scare the young 'uns.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about InnerClasses