File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mixing Inner class and Thead" Watch "Mixing Inner class and Thead" New topic

Mixing Inner class and Thead

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 392
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:
subject: Mixing Inner class and Thead
It's not a secret anymore!