• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Anonymous class issues

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this in the Marcus Green test:
Which of the following statements about this code are true?

I thought this would give an error because of the curly braces in for the anonymous class declaration. I did indeed test the code, and it works, but it also works when there is no curly braces. I have to say this is the first time I've seen an anonymous class written with curly braces, and am not sure what teh rule is, and why it works both ways. Can somebody tell me what is going on?
Damn, can't get this format right.
[ May 08, 2003: Message edited by: Wilson Mui ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The addition of the curly braces means that an anonymous class is being declared that extends Turing, but the anonymous class doesn't add anything new to the Turing type.
m.go(new Turing(){});
The go method accepts a Turning object or any subclass of Turing.
public void go(Turing t){
Since the anonymous subclass of Turning does not add anything new to the Turning type and because the go method accepts a Turning object or any subclass of Turing it really does not matter if the instance created in the first statement is an anonymous class or just and instance of Turning. For that reason, it doesn't make any difference if you remove the curly braces.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is for statement missing a closing ")" bracket?
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Woah that is the first time that I have ever heard of declaring an anonymous class that is extending a class. I know the example in the marcus green exam was somewhat of a trite example that didn't do anything. But I was wondering if anybody has had any experience where this was useful.
But I that is some obscure Java programming...for me at least.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wilson Mui:
But I was wondering if anybody has had any experience where this was useful.


please check out bruceeckel.com for "Thinking in Java" , it has got very good examples on this
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to be familiar with the construct of anonymous classes. It isn't because you will have lots of questions about them, rather you will see them used a lot as a space-saving measure in the real exam. (The examiners want to keep the code displayed in the window to a minimum in order to avoid excessive scrolling.) So, be prepared to see anonymous classes being used to do things like implement interfaces, a good example being Runnable in Threading questions.
[ May 09, 2003: Message edited by: Roger Chung-Wee ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic