Newbie tip Anonymous Inner Classes


#1

I just wanted to emphasize the warning at the bottom of page 21, concerning anonymous inner classes, listeners, and interface–it is indeed IMPORTANT. I thought I understood what was going on since the explanation in the book was succinct and easy to understand. However, the book only gives the best practice reason for using an anonymous inner class and not an explanation of how they work. This came back to bite me finally in chapter 11 when I didn’t understand the reason behind implementing the interface methods that aren’t actually used in ViewPager.OnPageChangeListener.
Rather than write a book here (which is what the author also wisely did by putting in the warning at the bottom of the page,) here is a URL to a great explanation of nested classes that includes anonymous inner classes, docs.oracle.com/javase/tutorial/java/javaOO/nested.html .
Don’t forget to click on the anonymous classes link in the section on inner classes.
Also, I found this URL on Abstract Methods and Classes, docs.oracle.com/javase/tutorial/java/IandI/abstract.html , to be very informative when trying to understand implementing an anonymous inner class. Also, this URL on interfaces, docs.oracle.com/javase/tutorial/java/IandI/createinterface.html , will help with understanding interfaces.


#2

You’re absolutely right about anonymous inner classes. AIC are the number one Java concept we find ourselves explaining in class. Most people are already familiar with anonymous functions, and AIC are totally different.

Thanks for posting the tip!