• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of non-static or Inner classes  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!