Hi all,
I'm new to the board and like many I plan to take the exam soon.
I had some doubts about question 7.2 on page 236 in the book regarding inner classes:
The question is:
Which of these statements concerning nested classes are true?
Select all valid answers.
(a) An instance of a top-level nested class has an inherent outer instance.
(b) A Top-level nested class can contain non-static member variables
(c) A Top-Level interface can contain non-static member variables
(d) A Top-leve nested interface has an inherent outer instance.
(e) For each instance of the outer class, there can exist many instances of non-static inner class.
The answer given is B, E
I thought that C should also be valid, but the authors state in the answers that "A top-level nested interface, just like package member interfaces, cannot contain non-static member variables"
However, upon
testing I was able to create a top-level nested interface, declare a non-static variable (int a), and call it from an instance of a class that implements the nested interface:
public class TopClass {
public static class TopLevelNestedClass {
public static interface TopLevelNestedInterface {
int a = 10;
}
public static class NestedNestedClass implements TopLevelNestedInterface {
}
}
static public void main(
String args[]) {
TopClass.TopLevelNestedClass.NestedNestedClass objInner = new TopClass.TopLevelNestedClass.NestedNestedClass();
System.out.println ("int a = " +objInner.a);
}
}
I'm scratching my head here- did I not understand the question correctly?
Thanks,
Charles