This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boone 1.1 Mock #5

 
Eric Barnhill
Rancher
Posts: 233
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 233
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, I've got this now, thanks to all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic