• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner Class - Help Required  RSS feed

 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Avinash Pandit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!