Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about anonymous question

 
michael wang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
michael wang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's very clear
thanks Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic