Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Anonymous inner class  RSS feed

 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • 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
  • 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: 288
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!