• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Use of non-static or Inner classes

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was going through a documentation on Inner classes.It was mentioned that one of the advantage of using Inner classes is that we can use them to access private member of the enclosing class.I agree that the ability of an inner class to access its enclosing class private member is an added advantage but in no way its seems to be the sole reason for using an Inner class.As always we have the option of using the public method for accessing that private field.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Subhendu Dash wrote:Was going through a documentation on Inner classes.It was mentioned that one of the advantage of using Inner classes is that we can use them to access private member of the enclosing class.I agree that the ability of an inner class to access its enclosing class private member is an added advantage but in no way its seems to be the sole reason for using an Inner class.As always we have the option of using the public method for accessing that private field.



Yes, but sometimes the design calls for not exposing those class internals to the general public.

Additionally, nested classes provide a way to keep closely coupled code physically close together, and save you from having another whole top-level class in a separate file for what is typically just a few lines of code.

Finally, as a somewhat more general picture of the access to private data, anonymous inner classes can be viewed as a first-order approximation to a closure. They combine a task to be executed with the context it needs to do its job.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic