Sponsored Links

Ad by Google
One of the important feature of java, most of the developer are avoid to using the anonymous classes and for beginners, It's difficult to understand the concept of anonymous class and their use at-least for me when I was doing the java classes.

What is Anonymous class
A class having no name is known as Anonymous class, Anonymous classes are expression, which means one can declare and instantiate a class at the same time.

When to use Anonymous class?
Use to keep you code short and simple.
Use them, if you want to use them only once.
For quick and dirty task.
How to use Anonymous class
searchBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
        // do search.
package com.javamakeuse.anonymous;

public class AnonymousExample {
 abstract class Flyable {
  abstract void fly();

 public static void main(String[] args) {
  AnonymousExample an = new AnonymousExample();
  Flyable flyableObj = an.new Flyable() {

   void fly() {
Note: You cannot declare static initializers or member interfaces in an anonymous class. Anonymous class can have static members provided that they are constant variables.
Sponsored Links


Post a Comment