How do I create a constructor for an anonymous class?

John Zukowski

Since anonymous classes don't have names, you cannot create a constructor by just using the name of the class. What you can do instead is to use an "instance initializer". Instance initializers basically look like methods with no names or return values:

ActionListener listener = new ActionListener {
  {
    System.out.println("Instance Initializer");
  }
  public void actionPerformed(ActionEvent event) {
    ...
  }
};
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact