I've recently started reading about Java Swing and lambda expressions. I read that you should always construct a frame by calling a method with java.awt.EventQueue.invokeLater()
like this:
public class SwingTests
{
private static void createGUI()
{
//creates frame, packs etc.
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createGUI();
}
}); //haven't really figured out formatting for this yet
}
}
Then my IDE suggested using a lambda expression instead of the anonymous class, so I switched to
java.awt.EventQueue.invokeLater(() -> createGUI());
but while reading, I also discovered method references and wanted to use those. However, replacing () -> createGUI()
with this::createGUI
gives a compiler error because "non-static variable this cannot be referenced from a static context". I discovered that SwingTests::createGUI
works, but referring to a class by name in its own body seems weird. Is there a better way for this?