• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boone 1.1 Mock #5

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind, I've got this now, thanks to all.
reply
    Bookmark Topic Watch Topic
  • New Topic