Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous class as parameter

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you use the curly bracers"{}", you create an anonymous class that extends Turing.
Francisco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic