• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Inner classes

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a begineer in java.
Can someone where exactly inner classes used in job.
 
Ranch Hand
Posts: 175
17
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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
 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76881
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic