Does Java support dynamic method invocation?

class A           { void F() { System.out.println("a"); }}
class B extends A { void F() { System.out.println("b"); }}

public class X {
    public static void main(String[] args) {
        A objA = new B();
        objA.F();
    }
}

Here F()is called dynamic, is not it?

This article says:

... Java bytecode does not support dynamic method invocation. There are three supported calling modes: invokestatic, invokespecial, invokeinterface, or invokevirtual. These modes allow calling methods with a well-known signature. We are talking about a strongly typed language. This allows you to do some checks directly at compile time.

Dynamic languages, on the other hand, use dynamic types. So we can call a method that is unknown when compiling time, but this is completely impossible with Java bytecode.

What am I missing?

+3
5

.

, , , , .

?

, Java B, objA B; , , B A, (objA F).

, , F, , , , .

: invokedynamic โ€‹โ€‹ Java7, JVM, script JVM, . , ( Grovvy MetaClass), Sun .

+13

, B A. ; .. B; . - - (, ).

, - .

+1

, "invokevirtual", .

.

+1

"", . ( ). , , - , .

"" (. Method). , , , ( , ).

. , , , / , Java .

0
source

You can create functional interfaces.

class Logger {
  private BiConsumer<Object, Integer> logger = null;

  // ...

  private Logger(Object logger) {
      this.akkaLogger = (LoggingAdapter) logger;
      this.logger = (message, level) -> {
          switch (level) {
              case INFO:  akkaInfo(message);
                          break;
              case DEBUG: akkaDebug(message);
                          break;
              case ERROR: akkaError(message);
                          break;
              case WARN:  akkaWarn(message);
                          break;
          }
      };
  }

  private Logger() {
      this.logger = (message, level) -> System.out.println(message);
  }

  // ...
}
0
source

Source: https://habr.com/ru/post/1740485/


All Articles