Author Topic:   Anonymous Class
Dadi
unregistered
posted March 04, 2000 08:28 PM           
Question

Which of the following statements are true?

1) An anonymous class cannot have any constructors
2) An anonymous class can only be created within the body of a method
3) An anonymous class can only access static fields of the enclosing class
4) The class type of an anonymous class can be retrieved using the getName method

Answer

1) An anonymous class cannot have any constructors

I think 1 & 2 should be the correct answers.

maha anna
bartender
posted March 04, 2000 09:28 PM             
Only ans 1 is correct. An anonymous class can be defined in static/instance floating blocks also.
regds
maha anna

import java.awt.event.*;

public class Test{
static {
ActionListener al = new ActionListener() {
{
System.out.println(getClass());//prints test$1
}

public void actionPerformed(ActionEvent e) {
System.out.println("Taking care of ActionEvent..");
}
};
}

public static void main(String[] args) {
new Test();
}
}

[This message has been edited by Tony Alicea (edited March 05, 2000).]

Tony Alicea
sheriff
posted March 05, 2000 07:20 AM             
About (2): A constructor is not a method and it can have anonymous classes declared in it.

Dadi
unregistered
posted March 05, 2000 08:55 AM           
Thanks Anna & Tony !

Jim Yingst
sheriff
posted March 05, 2000 11:12 AM             
An anonymous class can also be created when initializing a class or instance variable.

|