开发者

When to use inner classes? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Usage of inner class

Dear folks,

I am relatively new to java i am studying inner classes. i have read a little bit though, but i am not getting in what situation inner classes come handy for a programmer

Thank开发者_JAVA百科s in advance Vinayak Jog


Example : You have a class Car , which contains lots of objects.. One of them is really technical, and there are maybe 1-2 people in the world who know what this mysterious and wierd object is. Lets call this object type T

Now, if you are going to explain the class Car to anyone who does not know about T. Most of them understand that there is something that performs a useful function : F , but they have no idea that there is something called T.

This is what an inner class is, something that is required for the class that the inner class is in, but not required to be known by anyone else :)

Happy coding!


Inner classes are useful , when u don't want to expose the class existence to anything outside the containing class. I usually use Inner classes in making things more secure.

Also inner classes are used extensively when the class has no meaning outside the containing class, also if u need same name classes having different functionality or instances for different classes (the classes containing inner classes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜