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 Boone 1.1 Mock  #5 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 "Boone 1.1 Mock  #5" Watch "Boone 1.1 Mock  #5" New topic
Author

Boone 1.1 Mock #5

Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

<pre>
class Counter{
public static void main(String[] args){
Thread t = new Thread(new CounterBehavior());
t.start();
}
}
</pre>
Which of the following is a valid definition of CounterBehavior that would make Counter�s main() method count from 1 to 100, counting once per second?


b) This class is an inner class to Counter:
<pre>
class CounterBehavior implements Runnable {
public void run() {
for (int i = 1; i <= 100; i++);
try {
System.out.println(i);
Thread.sleep(1000);
} catch (InterruptedException x) {}
}
}
}
</pre>


this answer is wrong for the reason "b is not correct because the inner class is needed in main(), where there is no instance of the enclosing class (Counter)."
I'm confused on the access rules here. Do you have to instantiate an outer class, to reach an inner class, even from within the class? And if so, is it always or just for static methods? (My thinking is, you can have a custom adapter class, for example, and instantiate that from a method, without instantiating the outer class, so why doesn't this work? You can ignore these parentheses if they constitute a second question )
Thank You
Eric
Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

Never mind, I've got this now, thanks to all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boone 1.1 Mock #5
 
Similar Threads
Nested Classes
question about inner class instantiation
Hello Bartenders: A thread is lost
Inner class
SCJP mock test question - threads and inner classes