This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mixing Inner class and Thead Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mixing Inner class and Thead" Watch "Mixing Inner class and Thead" New topic
Author

Mixing Inner class and Thead

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
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

Joined: Aug 31, 2006
Posts: 226
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
 
subject: Mixing Inner class and Thead
 
Similar Threads
a threads question
Question on therad k&b 739 q.14
question of threads from Dan's mock
why output is 1 2 only?
Regarding Threads