• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested Classes

 
Greenhorn
Posts: 4
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I am a beginner in java, my question is as follows

What is the need for nested classes in java?

Can anyone give a real time example or simple program how it makes any difference when the same action is performed using an external class?

Thanks in advance...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jyothi sudi wrote:What is the need for nested classes in java?


Other than the fact that for things like anonymous classes, it's "the way Java does things", I'm not sure that you can really describe it as a 'need'.

What it is though is very useful.

Can anyone give a real time example


AbstractMap.SimpleEntry, which actually implements a nested interface - also very useful.

or simple program how it makes any difference when the same action is performed using an external class?


And that's where the problem comes: Any program that uses a nested class can be written to use a top-level class; but it won't be as good.

Quite apart from the question of why you would write Map.Entry as anything but a nested interface; the fact is that nested classes are allowed to do and "see" things that normal classes aren't, which helps with information-hiding.

To me, it also just makes sense: An Entry doesn't have any meaning except with a Map, so why not define it inside the class?

HIH

Winston
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might me useful to have subtypes subordinated to a given type.

Given a class representing a database package, subclasses can represent the sets of the output parameters of the very procedures.
Even if they are public classes, they can dwell in the source file of the enclosing class.

The other day I wrote a generator (in awk) that did this job. It was comfortable to generate only one output file.


Another example is Map.Entry. It is a useful type: look it up.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jyothi sudi wrote:Hi all, I am a beginner in java, my question is as follows

What is the need for nested classes in java?

Can anyone give a real time example or simple program how it makes any difference when the same action is performed using an external class?



One big difference between a nested class and a regular class, is that nested class has access modifiers. With it, you can create private class definitions, which in turn, allows you to prevent code outside of the outer class from instantiating specific types of objects.


For example, the java.util.Collections class has private List, Map, class definitions for the collections that are returned. And the definitions are private to prevent access -- heck, since it is private, it is not even documented by javadoc.

Henry
 
jyothi sudi
Greenhorn
Posts: 4
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all....
 
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic