OK, this is much more involved than is necessary to understand for the exam. But since you asked:
It's true that a class defined inside an interface is implicitly static, which means that it's a static member class (a so-called "static inner" class). However, this does not mean that it cannot make reference to non-static methods. It's true that you can't do this from a static
method or from a static
context, but (perhaps surprisingly) a static
class does not have this limitation. Observe:
<code><pre>
interface MyInterface {
void method1();
void method2();
// implicitly static class:
abstract class Abstract implements MyInterface {
public void method2() {
System.out.println("In method2()");
method1(); // See?
}
}
}
public class
Test {
public static void main(
String[] s) {
MyInterface mi = new MyInterface.Abstract() {
public void method1() {
System.out.println("In method1()");
}
};
mi.method2();
}
}
</pre></code>
The above code compiles and runs, printing out
<code><pre>
In method2()
In method1()
</pre></code>
The key point here is that the static member class Abstract contains a call to non-static method1(), declared (but not yet implemented) in the containing interface. Note that in order to do this, the class had to be declared to implement the containing interface. This meant I had to either (a) implement method1() and method2() in the class, or (b) declare the class abstract. I chose (b) because this way it's clear that method1() refers to the method1() declared in the interface, not to some overriding declaration. For comparision, here's option (a):
<code><pre>
interface MyInterface {
void method1();
void method2();
// implicitly static class:
class Implementor implements MyInterface {
public void method1() {
System.out.println("In method1()");
}
public void method2() {
System.out.println("In method2()");
method1(); // See?
}
}
}
public class Test {
public static void main(String[] s) {
MyInterface mi = new MyInterface.Implementor();
mi.method2();
}
}
</pre></code>
Here again, method2() contains an invocation of the method1() declared in the interface, but this isn't quite so impressive since the implementation of method1() is right there in the same static member class.
So back to the original question - answer 4 is incorrect. I can't imagine why you would ever want to do this though, and I'm sure that this detail won't be on the exam. So if the preceding explanation seems confusing, don't worry about it too much.