• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about InnerClasses

 
saravana kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ME have you take over JY's job of posting just seconds before me?
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic