I think your new question is big enough to merit a
thread of its own, so I have created a new thread for it.
In the good old days (up to Java7) we would write a class implementing the Runnable interface, maybe a package‑private class:-
...and in (I think) JDK1.2 they introduced
nested classes, so we would often write that as an anonymous class:-
Because you are inside the class, you can miss out
new Application().
Now, you are writing nine lines of code when you could get away with one line. Who wants all that code, if it is actually boiler‑plate, and the compiler can actually impute most of it? In Java8, they introduced the λ expression, which you can find out about in the tutorials link above. You can make
() -> startApplication() into a Runnable if you have a bit more information.
1: The invokeLater method takes a Runnable as its parameter. So the compiler knows it is trying to create a Runnable.2: Runnable is a FunctionalInterface. That means it has one “abstract” method. If the compiler creates a Runnable, it doesn't have to guess which method to implement, because there only is one: run().3: The run() method is not overloaded. I think you couldn't call an interface functional if it had an overloaded method.The compiler “knows” it has to create an instance of something implementing Runnable, and that there is only one method to implement. So let's see how much information it actually requires:-
So we have managed to reduce that to the list of parameters, and the one line which is the method body. Everything else will be the same in all instances, but the method body and the list of parameters might be different. So you need to keep those.Now, deleting the commented‑out code and some whitespace and the semicolon on line& leaves us with:-
Now, all you have to do is join the two halves of the method call together with the -> operator and Bob's your uncle
The right hand half of line 2 is a λ expression, which supplies all the information the compiler needs to assemble a Runnable object.
The syntax is different if you have multiple lines in the method body, or if you have different parameter lists.