Using Static Inner Classes to implement an interface - Java Object Oriented Design

Java examples for Object Oriented Design:interface

Introduction

A static inner class is similar to an inner class, but doesn't require an instance of the outer class.

Its basic form is the following:

class outerClassName {
  private static class innerClassName {
    // body of inner class
  }
}

Demo Code

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Main {
  public static void main(String[] args) {
    Timer t = new Timer(1000, new Ticker());
    t.start();/*  ww  w .  j a v  a 2  s .  com*/

    // display a message box to prevent the program
    // from ending immediately
    JOptionPane.showMessageDialog(null, "Click OK to exit program");
    System.exit(0);
  }

}

class Ticker implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    System.out.println("asdf");
  }
}

Related Tutorials