File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous class as parameter 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 as parameter" Watch "Anonymous class as parameter" New topic

Anonymous class as parameter

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
I know, you can not create a constructor since it doesn't have a name. But here, how is it possible to passible to pass anonymous class as a parameter?
public class Morecombe{
public static void main(String argv[]){
Morecombe m = new Morecombe();
m.go(new Turing(){});
public void go(Turing t){
class Turing extends Thread{
public void run(){
for(int i =0; i < 2; i++){

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Thiru,
It is the same as doing the following assignment in java:
Turing t = new Turing(){};
That is essentially what the compiler does with the code you have shown.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Turing t = new Turing(){};
What do the curly brackets mean? The Turing class is already defined, so does this definition get overriden or something?
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
when you use the curly bracers"{}", you create an anonymous class that extends Turing.

Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="" target="_blank" rel="nofollow">here</a> to see an example.
I agree. Here's the link:
subject: Anonymous class as parameter
It's not a secret anymore!