Anonymous Class – Some quick facts

  • An Anonymous class is a class not given any name.
  • It is declared and instantiated in a single line
  • You should consider using anonymous when you need to create a class that will be instantiated only once.
  • An Anonymous class can implement an abstract class or interface without using extends or implements keyword, where it will be implementing the methods inside the body
  • An anonymous class cannot have a constructor. So you cant send any parameters
  • An anonymous class can access any variables visible to the block within which it is declared.
  • An anonymous class can also access the methods of the class that contains it.

Runnable r = new Runnable(){
public void run(){
//implementation for run
}
}

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s