a rather long-winded title!
here is the question from JDiscuss that has prompted this question...
Given the following class:
class Counter
{
public int startHere = 1;
public int endHere = 100;
public static void main(
String[] args)
{
new Counter().go();
}
void go()
{
// A
Thread t = new Thread(a);
t.start();
}
}
What block of code can you place at line A above so that this program will count from startHere to endHere?
A.
Runnable a = new Runnable()
{
public void run()
{
for (int i = startHere; i <= endHere; i++){ System.out.println(i); }
}
};
B.
a implements Runnable
{
public void run()
{
for (int i = startHere; i <= endHere; i++){ System.out.println(i); }
}
};
C.
Thread a = new Thread()
{
public void run()
{
for (int i = startHere; i <= endHere; i++){ System.out.println(i); }
}
};
ANSWERS
A
B
C
Only A.
Only C.
the answer is A and C.
ps- in each example, are they trying to create an anonymous implementing (of Runnable) or subclass (of Thread) class by putting the curly braces in before the semicolon? is it an argument-local inner class?