Java Magazine, Sept/Oct 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 51 java 9 11 dup 12 invokespecial 5 Method scratch Bear init V 15 astore_ 2 16 aload_ 1 17 invokevirtual 6 Method scratch Cat groom V 20 aload_ 2 21 invokevirtual 7 Method scratch Bear groom V 24 aload_ 1 25 astore_ 3 26 aload_ 3 27 invokeinterface 8 1 InterfaceMethod scratch Furry groom V 32 aload_ 2 33 astore_ 3 34 aload_ 3 35 invokeinterface 8 1 InterfaceMethod scratch Furry groom V The two calls at 27 and 35 look like they are the same but they actually invoke diferent methods The call at 27 will invoke Cat groom whereas the call at 35 will invoke Bear groom With this background on invokevirtual and invokeinterface the behavior of invokespecial is now easier to understand If a method is invoked by invokespecial it does not undergo virtual lookup Instead the JVM will look only in the exact place in the vtable for the requested method This means that an invokespecial is used for three cases private methods calls to a superclass method and calls to the constructor body which is turned into a method called init in bytecode In all three cases virtual lookup and the possibility of overriding must be explicitly excluded Final Methods There remains one corner case that should be mentioned the case of final methods At first glance it might appear that calls to final methods would also be turned into invokespecial instructions However section 13417 of the Java Language Specification has something to say
You must have JavaScript enabled to view digital editions.