• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Inner Class - Help Required

 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys !

I am not able to understand this code for inner class. If anybody can please help me in this as to what will be the flow of this Code.



Any comments Appreciated.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable retValue is assigned an instance of a new subclass of DBIterator; the subclass is defined right there. The code



does pretty much the same thing as

 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dude !

But what I couldn't understand is the flow of execution. as to wheather all the methods will be executed one after another or how will it go.

Cheers,
Rahul
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
None of them will be executed until someone takes this new object pointed to by "retValue" and calls them. As this object is the return value of this method, we don't see any of its methods being called here (well, except that "toString()" is implicitly called in the println() call.)
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

Any more comments ? Anybody in the Board.

Cheers,
Rahul
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everybody,

I have traced through the logs and now I can very well understand what is the execution flow of this code.

The other thing which is coming up in my mind is what is the benifit of doing this. If instead of defining these methods here, we could also define these methods as private in the DBIterator Class also. But why this way ?

Cheers,
Rahul
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also interested in the answer of why would we go for inner classes as defining outer class serves the same ?

-Avinash
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this particular case I see no advantage to using this inner class. You could just as well use a top-level class, and the code would be clearer (i.e., we never would have had this discussion!) The top-level class's constructor would need three arguments: startDate, endDate, and longPK would need to be passed in.

In other cases, though, if a class is short and needs to use member variables of the enclosing class, it makes sense to use an inner class. By "short" I mean less than ten lines, generally.
 
reply
    Bookmark Topic Watch Topic
  • New Topic