Q1 Which of the following statements are true? A) An anonymous class cannot have any constructors B) An anonymous class can only be created within the body of a method C) An anonymous class can only access static fields of the enclosing class D) An anonymous class instantiated and declared in the same place.
i think the answer is ABD however the answer given is AD why? Q2 Is there any difference between anonymous inner class and anonymous class? thanks [ March 04, 2002: Message edited by: michael wang ]
You can use an anonymous class inside a constructor, which is not a method. Or how about this example:
There's two examples that prove B is false.
An inner class is a class defined inside the instance of another class. An anonymous class just means you are creating an instance of a subclass of a class without giving the subclass a name. So they are two different things. That being said, you can't define a top-level anonymous class; the only place you can define an anonymous class is within something else. So in practice, your anonymous class is always going to be a nested class.