File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Boone 1.1 Mock #5
 
Similar Threads
Hello Bartenders: A thread is lost
question about inner class instantiation
SCJP mock test question - threads and inner classes
Nested Classes
Inner class