This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous class question

 
anrup kris
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 318
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic