File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mixing Inner class and Thead

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading K&B book sequentially.

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 ]
 
Tim LeMaster
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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").

So the output is " MyThread foo"
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic