This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes  RSS feed

 
sivanagaswathi kallam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a begineer in java.
Can someone where exactly inner classes used in job.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an inner class if you need to accurately represent composition in your code because an instance of an inner class cannot exist in isolation.

Which relationship can be defined as a composition depends on your organization. For example, in a company that sells car parts, a Car has-a Wheel relationship may be defined as an aggregation (not a composition) because a Wheel can be sold on its own. However, in a car racing game company, a Car has-a Wheel relationship may be defined as a composition because a Wheel cannot exist on its own i.e. a Wheel class must be defined as an inner class inside a Car class.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sivanagaswathi kallam wrote:Can someone where exactly inner classes used in job.

Just to add to Joe's excellent explanation: In addition to true "inner" classes; ie:
public OuterClass {
  public InnerClass {


you also have nested classes, viz:
public OuterClass {
  public static InnerClass {


and these are more common - and also more usually public.

Basically, it's just a regular class that only has a context when used with the "outer" class - as, for example: Car.Wheel - because if you just defined it on its own, Wheel could be a wheel for anything.

HIH

Winston
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To extend these two excellent explanations, there are also different categories of inner classes. The 'composition' explanation applies to all of them, but if you make a static inner class, it 'resembles' (my term) an outer class more, in that it can be constructed in the absence of the inner class. It can still be thought of as being a constituent part, and closely bound to the outer class, but you could create instances of it from without--possibly as parameters or return values for methods on the outer class. The other, non-static inner classes (anonymous and otherwise) will have a special relationship with the outer class, in that they can reference all its members, and you can have a special Outer.this reference.

Inner classes see a lot of use for event handling, BTW, but there is a fairly new concept called "lambdas" which people are using lately, and which can cut down some of the boilerplate text surrounding the use of anonymous inner classes.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have seen the Java™ Tutorials about nested classes?
Example. You have a tree (=kind of data structure). The tree consists of nodes. The Node<E> class is a (private) inner class inside the Tree<E> class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!