Hi all, i need a real time application or implementation to understand the property of inner class. i read and got the semantics of inner class by using MyOuterClass and MyInnerClass. But i need some real time scenario where we can implement Inner Class to represent that Entity.
Do you mean "real time" as in real-time computing or "real world", meaning a concrete example? If you mean the latter, I once used an inner class to implement a linked list. Linked lists are made up of node which contain a data reference and pointers to other nodes in the list. Since the user of the linked list is interested in the data in the list and not the implementation of the list itself, I made the node class an inner class.
You can declare anything to be an inner class. The question remains: should it BE an inner class? I think you have to clear up the relationship between your classes before you can answer that. Have a look at the Java Tutorial chapter on Nested Classes for some more examples.
You would use an inner class only if you are dealing with an object that has no use or meaning outside of its outer class and is complex enough to merit its own objectification (for a lack of a better term). It's difficult to find a general example because it really depends on your application, but perhaps an Address class could have a PhoneNumbers inner class which holds all the phone numbers associated with the address and implements some extra functionality, like returning the best phone number, the most used one and whether they are all in the same area code. You need a class to do all the extra nifty things, but you don't really need it in a separate class, since you do not need (or want) to deal with phone numbers outside of the context of an Address.
Post by:autobot
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
a bit of art, as a gift, that will fit in a stocking