Hi all,
I sent that message at Kathy's e-mail address mentioned in the book, but received no response. I also searched the archive of that forum if someone had mentioned that before, but found nothing, so I decided to put it here.
In the self
test area for chapter 8 - Inner Classes I found the following question:
"1. Given the following,
public class MyOuter{
public static class MyInner{
public static void foo(){}
}
}
which statement if placed in a class other then MyOuter or MyInner, instantiates an instance of the nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. MyOuter m = new MyOuter();
MyOuter.MyInner mi = m.new MyOuter.MyInner();
D. MyInner mi = new MyOuter.MyInner();"
In the answer section it says that the correct answer is A, and this is correct, but in my oppinion B, and D are also correct. To prove it I wrote the following test:
// firstly the class from the question #1 chpt8
package scjp.chpt08;
public class MyOuter{
public static class MyInner{
public static void foo(){}
}
}
// in another package
package scjp.testit;
import scjp.chpt08.MyOuter;
import scjp.chpt08.MyOuter.*; /* you can do this, and it makes sense if there are inner classes in MyOuter */
public class OuterInnerTest {
public static void main(
String[] args) {
MyOuter.MyInner in1 = new MyOuter.MyInner();
MyOuter.MyInner in2 = new MyInner();
MyInner in3 = new MyOuter.MyInner();
// you can even add the line below and it will work too.
MyInner in4 = new MyInner();
}
}
So, the requirements for that question are met - the statement is placed outside MyOuter/MyInner (in another package as a matter of fact) and both "B" and "D" work (at least my Eclipse framework with JDK 1.4.2_02 instaled does not protest about it).
And the same about the "two minute-drill" for that chapter - I mean the key point before the last one. In the light of the above example seems not to be true.
Anybody has an idea how would that answer be marked on the real exam???
Regards
Chris