I have often come across this snippet :
{
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
new tester(); // class name
}
});
}
I know why are we using this,but cannot understand how it is going.I mean i dont understand this snippet.
(We are initializing object under run method,,,why?? )
Please explain this
With that bit of code you’re creating a
Inner Classthat implementsRunnable, that instance will be enqueued in the AWT processing task dispatcher for later processing in a thread. Quoting the documentation,invokeLater…So at some point, the AWT dispatcher will decide to run that instance of Runable in a thread. That will provoke the execution of the method
runand therefore the execution of the statementnew tester();, which simply create an instance of the classtester.To your specific question …
It really doesn’t seem right to just create a class in the
runmethod, unless the constructor is doing lots of things which is actually a bad practice.It’be much more intuitive to do something like :