• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous class and the semi colon

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After every declaration of an anonymous class are we required to add a semi colon?

Or is it only in some cases?

Thank you
david
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there are cases where no semicolon is used after the declaration of an anonymous class. An example is when an instance of an anonymous class is passed as an argument into a method.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi david,

Suppose you are creating an anonymous class which implements the interface, ActionListener like below,

public void aMethod(){

//....

new ActionListener(){

//Anonymous class code
}
}
In the above case you dont need any semicolon after the anonymous class.

If you modify the same code as below,

public void aMethod(){

//....

ActionListener a1 = new ActionListener(){

//Anonymous class code
};
}
In the above modified code, if you are assigned the instance of your anonymous class to the reference variable say, a1 in this case then you have to use semicolon after the anonymous class.
 
reply
    Bookmark Topic Watch Topic
  • New Topic