• 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

Question regarding inner class

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B book we have the question:

Which constructs an anonymous inner class instance?

Runnable r = new Runnable() {public void run{}};

System.out.println(new Runnable() {public void run() { }});


They have given the answer is second one.

Please expalin how come?

Thanks.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I think both constructs are ok cos these two are all ways to create and anonymous class
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vardhan,

The 2nd answer is correct.Anonymous class is always declared and instantiated on the fly.The 1st option is that of a normal object creation.

Regards

Nikhil
 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mambe/Nikhil Thanks for your replies.

If we kept aside the ananymous class concept, is the first one right or wrong ?

Thanks.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runnable r = new Runnable() {public void run{}};
>>>run{} isn't ok for a method declaration=>run(){} is the way it must

System.out.println(new Runnable() {public void run() { }});
>>>this one is the right one..., its just good looking for the brackets and ';' and () lol...
:-)
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i forgot :
the first one is ok, only the synthax is wrong...
Runnable r = new Runnable() {public void run>>>{}<<<};

Runnable r = new Runnable() {public void run>>>()<<<};
this is allowed...;-)
[ March 27, 2006: Message edited by: Reper Arno ]
 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reper,


i forgot :
the first one is ok, only the synthax is wrong...
Runnable r = new Runnable() {public void run>>>{}<<<};

Runnable r = new Runnable() {public void run>>>()<<<};
this is allowed...;-)



Do you mean to say first one is right or wrong?

Thanks.
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like they write it, its wrong...they omitted the ()

ups i forget a thing
Runnable r = new Runnable() {public void run(){}};

do you understand what i mean?

[ March 27, 2006: Message edited by: Reper Arno ]
[ March 27, 2006: Message edited by: Reper Arno ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think 2nd option is more correct
 
reply
    Bookmark Topic Watch Topic
  • New Topic