A) An anonymous class cannot have any constructors
My ans: TRUE
Constructors have same name as that of the class. Now, anonymous classes have no name and hence u do not provide any constructors for it. In fact, the compiler actually creates a constructor behind the scenes.
B) An anonymous class can only be created within the body of a method
My ans: FALSE
Actually an anonyous class declaration is always associated with a class instance creation. i.e.
class
Test {
Button B = new Button(){ --override some method/do something-};
}
Out here, u can see an anonymous class is created and it overrides some method in the Button class. Further, the anonymous class declaration here is associated with the Button b instance creation.
C) An anonymous class can only access static fields of the enclosing class
My ans: FALSE
Remember that anonymous class are basically inner class(mind u NOT static inner classes). Therefore, inner classes cannot mark a variable static. It can do so provided they are marked final.
For eg:-
class UnrelatedClass
{
static int k = 100;
}
class Outer
{
class Inner extends UnrelatedClass
{
static int i = 10; // compiler error!
static final int i = 10; // compiles fine
void print()
{
System.out.preintln("Value K is inherited and it is:"+ k);
}
}
}
Also, note that an inner class can inherit static member as shown in above example but not declare them. They can do so provided they are marked final.
D) An anonymous class instantiated and declared in the same palce.
My ans: TRUE
They are declared and instantiated in the same place. Write a small piece of code and u'll see this point.
Also, remember the following:
Due to point (D) and since anonymopus classes are associated with class instance creation, therefore anonymous classes are NEVER ABSTRACT OR STATIC. Plus they IMPLICITLY FINAL.
Hope this helps :-)
-sampaths77