Hi,
This is one of the question in nested classes of K&B
public class Foo
{
Foo() {System.out.print("Foo");}
class Bar{
Bar() {System.out.print("Bar");}
public void go() {System.out.print("Hi");}
}
public static void main(
String args[]){
Foo f = new Foo();
f.makeBar();
}
void makeBar(){
(new Bar() {}).go();
}
}
The result of the above code is given as "FooBarHi".
But as the makeBar method, while invoking new Bar(){} it is creating a new empty subclass of Bar class rather than invoking the constructor of Bar class. As the go() method is invoked on anonymous empty subclass, it should give a compile error.
Please clarify me if I have interpreted the code wrongly or if my assumption is wrong.
Thanks
Sharanya