• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes  RSS feed

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still dont get why one would use inner classes. why cant we just instantiate an object of that kind using new operator and use it. can someone please help me out???
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner classes are used for situations where an object can not exists without another object. For instance a Map has Map.Entries (both interfaces but for this example you can view them as classes). The Map.Entry cannot exists without a Map. It has no purpose to live without it. Therefor it's made an inner interface.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You *can* instantiate an instance of "that kind".

It's also use when nothing but the outer class needs or cares about the class.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
There are several compelling reasons for using nested classes, among them:
* It is a way of logically grouping classes that are only used in one place.
* It increases encapsulation.
* Nested classes can lead to more readable and maintainable code.


PS: Non-static nested classes are called inner classes.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look into this article. very good. inner classes
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also a great article here, on JavaRanch.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!