Possible Duplicates:
Efficiency of Java “Double Brace Initialization”?
Meaning of new Class(…){{…}} initialization idiom
Assume I created a JMenu Bar the following way:
JMenuItem saveMenuItem = new JMenuItem("Save")
{{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String location = GUI.Custom.QuickDialogs.selectFile(false);
try
{
PrintWriter pw = new PrintWriter(new File(location));
String text = textArea.getText();
pw.println(text);
pw.flush();
pw.close();
}
catch(Exception ex)
{
textArea.append("Could not save this debug output");
}
}
});
}};
JMenu optionsMenu = new JMenu("Options")
{{
add(saveMenuItem);
setVisible(true);
}};
private JMenuBar menuBar = new JMenuBar()
{{
add(optionsMenu);
setVisible(true);
}};
Is this a bad design pattern to create objects this way as opposed to just declaring the variable, and then creating in a constructor or something?