Nowhere in the chapter 8(Inner Class) it's mention that an anonymous inner class can be inside a method too and also when the constructor will be invoked BUT in question 14 of chapter 9(threads) it uses these concepts.
I am still not clear with this funda. please help me understand this concept. How can one know about these if reading K&B sequentially ?
[HENRY: Formatted code] [ October 26, 2006: Message edited by: Henry Wong ]
Joined: Aug 31, 2006
You have to combine your knowledge. You know when constructors run, inner classes anonymous or not do not violate these rules.
So you create a new anonymous subclass thats extends MyTread and overrides public void run().
When the anonymous class is instantiated the constructors in it class heirarchy must run. So Object's constructor run, then Thread's, then MyThread's (which prints " MyThread"). Then start is called on the new instance which Thread calls run(). run() has been overridden by MyThread but then further overriden by the anonymous class (prints " foo").