When I compile this simple code
class Main {
public void execute() { }
public static void main(String[] args) {
Runnable run = new Main()::execute;
}
}
I get the following byte code for the main method:
0 new user.Main [2]
3 dup
4 invokespecial user.Main() [3]
7 dup
8 invokevirtual java.lang.Object.getClass() : java.lang.Class [4] // <--- ???
11 pop
12 invokedynamic 0 run(user.Main) : java.lang.Runnable [5]
17 astore_1 [run]
18 return
I'm wondering why the compiler decided to emit an invokevirtual java.lang.Object.getClass()
followed directly by a pop
. What's the purpose of this seemingly useless call?