• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

anonymous inner class

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All:
This question is from Javacaps Mock Exam.
Which of the following statements are true ?
a. An anonymous inner class cannot have any constructors.
b. An anonymous inner class can be created only inside a method.
c. An anonymous inner class can only access static fields of the enclosing class.
d. An anonymous inner class can implement an interface.
I think
a)true.
b)true. NOT SURE
c)false. It can access static as well as not-static fields of the enclosing class.
d)true. In the below code anonymous inner class is implementing ActionListener interface
button1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
.....
}
}
);
Please let me know if I'm wrong.
Thanks
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I feel that the answers you have entered are perfectly right.
Cheers
Venu
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think d is true too. But I think b is false. pls see:
Thread t = new Thread(new Runnable() {
public void run() {
//....
}
});
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yiqinye,
Don't you think the code you mentioned must reside in some method/constructor ?
I don't know may be I'm moving in wrong direction.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from http://developer.java.sun.com/developer/Books/certification/page5.html
Some classes that you define inside a method do not need a name. A class defined in this way without a name is called an anonymous class. Clearly you cannot use new in the usual way to create an instance of a class if you do not know its name. In fact, anonymous classes are defined in the place they are constructed:
Ajit, Maha Anna, Paul any comments on this issue ?
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the anonymous class implementing Runnable interface i defined is not inside a method but belongs to an object of the enclosing class in which the reference variable Thread t was declared. do u agree?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds perfect....
public class Class1 {
Thread t = new Thread(
new Runnable() {
public void run() {
System.out.println("In run method");
}
}
);
public static void main(String args[]) {
Class1 c = new Class1();
}
Class1() { t.start();}
}
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only (a) and (d) are the right answers.
Dilip, I can help you, but what is the issue here you are confused with?
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
I was thinking that anonymous inner class can be created "only inside a method".
But yiqingye made me clear that it can be created outside method also.
Thanks for quick response.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With regard to point (b):
I believe the sense of "inside" intended by the question refers to lexical scope, so it asks whether you can write
"... new Button() { blah blah };"
anywhere that is not within the outer {} of a method declaration.
And Dilip was on the right track before he got convinced to give up. I think the answer to the question is: False. You can write the above in a constructor as well (which is not technically a method) and in a static initializer too (which is not a method at all).
yiqingye got off on another idea entirely, I think because he understood "inside" in terms of the structural organization of the runtime data. But I don't understand what he meant well enough to say whether I think it's right.
jim goodwin

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic