• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Anonymous inner class

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain ,How this code is executing ?when there is no semi colon after anonymous class definition. at line 1.this is from john meyers mock exam.


 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

the code execute perfectly well. look where yuo are declaring the class: it is like a common parameter inside a method calls.
you would never write something like

right?

you are overriding the already existing class Face (please use the capital letter for classes!) not declaring a brand new class; to test this yuo can delete the Face class declaration (not the inner anonymous class) and see a compilation error.

Just last not do not misunderstand anonymous inner classes with inner classes: in this case you need the ";"


because you are closing the line declaration.

Ciao
Maurizio
 
raj malhotra
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it. syntax for Argument-Defined Anonymous inner class to end is });
Thanks .
please use the capital letter for classes!
by the way this question is straight from Mock exam


 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, ranchers!


the line
seth( new face(){} ); // 1

invokes the static method seth that takes a face as argument.
You also can just pass a new instance of face, the normal way would be
seth( new face() ); // 1'

The only difference in line 1 and 1' is that in 1, you use an anonymous class.

Normally the indentation would be like


you may read this like this pseudo


Since this anonymous class doesn't override anything, line 1 and 1' just do the same.


Yours,
Bu.
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic