aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous class issues 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 issues" Watch "Anonymous class issues" New topic
Author

Anonymous class issues

Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
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 ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Is for statement missing a closing ")" bracket?
Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
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.
aadhi agathi
Ranch Hand

Joined: Apr 29, 2002
Posts: 263
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


Aadhi
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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 ]

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
jQuery in Action, 2nd edition
 
subject: Anonymous class issues