aspose file tools*
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
Author

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){
t.start();
}
}
class Turing extends Thread{
public void run(){
for(int i =0; i < 2; i++){
System.out.println(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.
Regards,
Manfred.
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


Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous class as parameter