2

I have observed, that after compiling an interface containing default method definition, like:

interface Delta {

    default void someMethod() {
        System.out.println("Hi.");
    }

}

and after disassembling the respective .class file (including only corresponding snippet here):

javap -v Delta.class

####

{
  public void someMethod();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #1                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #2                  // String Hi.
         5: invokevirtual #3                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 10: 0
        line 11: 8
}

default modifier is gone.

Could anyone explain - why?

I am running:

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

Note, that I am aware, that all methods are implicitly public, when no modifier is defined. So, I am not asking why public modifier is present in the compiled version of the file.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • what java version, obviously. might be a bug in `javap` - since I am on jdk-16 and I do see it. – Eugene Jul 30 '21 at 19:30
  • @Eugene good point. It's `openjdk version "11.0.2" 2019-01-15`. – Giorgi Tsiklauri Jul 30 '21 at 19:31
  • The question is really why `javap` doesn't show the `default` modifier. The modifier itself is source code, not bytecode, so it wouldn't be literally present in the compiled bytecode (and the fact that the method isn't marked as `abstract` is enough to know it has a default implementation). – kaya3 Jul 30 '21 at 19:32
  • @kaya3 then why all other modifiers are present in the disassembled one? I would really appreciate if you could just try to help me understand my point, without just rushing into demonstrating *how wrong my question is*. – Giorgi Tsiklauri Jul 30 '21 at 19:33
  • eh, sorry, don't have the same exact version, but on a higher 11 (`11.0.9.1`), I see it. On Mac, btw. – Eugene Jul 30 '21 at 19:33
  • @GiorgiTsiklauri I did read your question, and I'm telling you this is to do with how `javap` produces its output, not to do with what is or isn't present in the compiled `.class` file. But I don't have an answer for why `javap` doesn't print the word `default` in its output, so that is the question needing an answer. – kaya3 Jul 30 '21 at 19:35
  • 1
    [this?](https://bugs.openjdk.java.net/browse/JDK-8235200) – Eugene Jul 30 '21 at 19:36
  • @kaya3 well, that's all my question is about - why `default` is missing; however, I can't be sure, that it's due to `javap` and `javac`.. or maybe I should be sure? – Giorgi Tsiklauri Jul 30 '21 at 19:37
  • Yes, I got that, and I'm telling you that I am sure `javap` is responsible for this, not `javac`. – kaya3 Jul 30 '21 at 19:38
  • @kaya3 I see. Thanks. – Giorgi Tsiklauri Jul 30 '21 at 19:39
  • @Eugene looks interesting.. so, it seems to be a `javap 11.0.2` bug then. – Giorgi Tsiklauri Jul 30 '21 at 19:40
  • Note that methods are not generally implicitly public; rather, _all members_ of an interface are public. – chrylis -cautiouslyoptimistic- Jul 30 '21 at 20:48
  • @chrylis-cautiouslyoptimistic- wrong. You can define `private` instance method. :) – Giorgi Tsiklauri Jul 30 '21 at 21:08

1 Answers1

4

Surely looks like a javap bug, see this defect. I've encountered a few more of these javap issues when new features where added, just FYI.

kaya3
  • 47,440
  • 4
  • 68
  • 97
Eugene
  • 117,005
  • 15
  • 201
  • 306