aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes anonymous class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous class question" Watch "anonymous class question" New topic
Author

anonymous class question

anrup kris
Greenhorn

Joined: Oct 22, 2000
Posts: 17
I found this question in a Javacaps mock paper.
1) Can anyone tell me whether anonymous classes can be created anywhere else except within a method?
2) What is the difference b/w an anonymous class and an anonymous inner class.I thought both meant the same
I had checked (a),(b) and (d). The correct answers are (a) and (d).
15. 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.
thanx
-kris
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Hi anrup.I am of the opinion that answers A) and D) are correct.
A) An anonymous class cannot have any constructors - TRUE.Since an anonymous class does not have a name , obviously u cannot have a constructor for it.
B) An anonymous class can only be created within the body of a method - FALSE.An anonymous class can also be defined within a class scope , but it wont be much good if u wont have a reference to it.
C) An anonymous class can only access static fields of the enclosing class - FALSE.There is no such restriction.
D) An anonymous class instantiated and declared in the same place - TRUE. This is normally the case.

------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
hi anrup,
1) Can anyone tell me whether anonymous classes can be created anywhere else except within a method?
--> Anonymous classes can be created in static/instance floating blocks also.
import java.awt.event.*;
public class Sample{
static {
ActionListener al = new ActionListener() {
System.out.println("I am in ActionListener!!");
}
public void actionPerformed(ActionEvent e) { System.out.println("Taking care of ActionEvent..");
}
};
}
public static void main(String[] args) {
new Sample();
}
}
Hope this helps.
anrup kris
Greenhorn

Joined: Oct 22, 2000
Posts: 17
Thanx Sunitha and Uday for clearing my doubts.
Btw where can I find access to such info.Can you give some good online references?
thankx
-kris
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
Ummm...Sunitha, I don't think in the example you gave, you used an anonymous class to represent your point. I mean, first of all, you used a constructor, and second of all it had a name. Doubly not anonymous. In fact, that class could almost be considered famous.
UPENDRA KANE
Greenhorn

Joined: Sep 29, 2000
Posts: 24
Matt,
Sunita's example looks perfectly alright to me.
You say, she used constructor. Constrctors to have same name as class. Where is that her code?
& the name you are referring to is the Interface reference.
Please fire responses if you guru's there think I am wrong...


Regular
 
 
subject: anonymous class question