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
posted
0
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
posted
0
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
posted
0
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.