• 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

new keyword with Runnable

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
return new Runnable() {
public void run() {
code...
}

I have seen a code like this. My question is: as Runnable is an interface how we can use it with the new keyword.
As new keyword can be used to creae an object how we can create an object for an interface Runnable.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is an anonymous class. You are creating a class maybe called Foo$1 which implements the interface. You must implement the run method in order for it to compile.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's worth to remember that an anonymous class does not have do be based on interface .
You can use any class/interface/
The class can't be final and must have a constructor that is accessible. In other words: you need to be able to extend that class.

An example:
 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is an anonymous class. You are creating a class maybe called Foo$1 which implements the interface. You must implement the run method in order for it to compile.



so, is it like if we are creating an anonymous class, new keyword can even be used with an interface.

can anyone explain in detail what happens if we are using a new keyword for an interface while creating an anonymous class?

Regards
Gerald.J
 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is an anonymous class. You are creating a class maybe called Foo$1 which implements the interface. You must implement the run method in order for it to compile.



Hi,

Thanks for your reply.

This is the code block I came across.

private final Runnable createRemoveRunnable(final String name) {
return new Runnable() {
public void run() {
objects.remove(name);
expire.remove(name);
}
};
}

Regds
Gerald.J

 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, Anonymous class can even consider an interface as class. It even creates an object for an interface?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gerald Amalraj wrote:So, Anonymous class can even consider an interface as class. It even creates an object for an interface?


No. It does not allow to treat interface as a class. It creates a new object of a class with no name (thus anonymous). And this class implements an interface.

You can read more about it here.
 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic