Why Kotlin decompiler generates null.INSTANCE












0















I am trying to understand few of the Kotlin features by checking how it looks in Java side.



So as an experiment, I tried with this:



val printKotlin = fun () {
print("Hello Kotlin")
}


So the output for the above snippet is:



public final class FunAsVariableKt {
private static final Function0 printKotlin;

public static final Function0 getPrintKotlin() {
return printKotlin;
}

static {
printKotlin = (Function0)null.INSTANCE;
}
}


How do I understand the static block of the above decompiled code? Why it is generating this non working code?










share|improve this question

























  • What do you mean by "the output", output of what? The class bytecodes decompiled into Java?

    – Jayson Minard
    Nov 20 '18 at 1:47
















0















I am trying to understand few of the Kotlin features by checking how it looks in Java side.



So as an experiment, I tried with this:



val printKotlin = fun () {
print("Hello Kotlin")
}


So the output for the above snippet is:



public final class FunAsVariableKt {
private static final Function0 printKotlin;

public static final Function0 getPrintKotlin() {
return printKotlin;
}

static {
printKotlin = (Function0)null.INSTANCE;
}
}


How do I understand the static block of the above decompiled code? Why it is generating this non working code?










share|improve this question

























  • What do you mean by "the output", output of what? The class bytecodes decompiled into Java?

    – Jayson Minard
    Nov 20 '18 at 1:47














0












0








0








I am trying to understand few of the Kotlin features by checking how it looks in Java side.



So as an experiment, I tried with this:



val printKotlin = fun () {
print("Hello Kotlin")
}


So the output for the above snippet is:



public final class FunAsVariableKt {
private static final Function0 printKotlin;

public static final Function0 getPrintKotlin() {
return printKotlin;
}

static {
printKotlin = (Function0)null.INSTANCE;
}
}


How do I understand the static block of the above decompiled code? Why it is generating this non working code?










share|improve this question
















I am trying to understand few of the Kotlin features by checking how it looks in Java side.



So as an experiment, I tried with this:



val printKotlin = fun () {
print("Hello Kotlin")
}


So the output for the above snippet is:



public final class FunAsVariableKt {
private static final Function0 printKotlin;

public static final Function0 getPrintKotlin() {
return printKotlin;
}

static {
printKotlin = (Function0)null.INSTANCE;
}
}


How do I understand the static block of the above decompiled code? Why it is generating this non working code?







kotlin jvm-bytecode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 1:53









Jayson Minard

40.2k17112173




40.2k17112173










asked Nov 20 '18 at 1:23









Chandra SekharChandra Sekhar

12.2k125693




12.2k125693













  • What do you mean by "the output", output of what? The class bytecodes decompiled into Java?

    – Jayson Minard
    Nov 20 '18 at 1:47



















  • What do you mean by "the output", output of what? The class bytecodes decompiled into Java?

    – Jayson Minard
    Nov 20 '18 at 1:47

















What do you mean by "the output", output of what? The class bytecodes decompiled into Java?

– Jayson Minard
Nov 20 '18 at 1:47





What do you mean by "the output", output of what? The class bytecodes decompiled into Java?

– Jayson Minard
Nov 20 '18 at 1:47












1 Answer
1






active

oldest

votes


















6














Use the Kotlin bytecode inspector to see what is being generated as JVM bytecode rather than trying to decompile bytecode to Java, which likely won't work for code generated by a different language compiler because it might not follow the expected patterns. A bytecode decompiler does not always generate working code.




JVM bytecode != Java language specific



Kotlin generated JVM bytecode != Java generated JVM bytecode




The NULL instance you are seeing is a misinterpretation by your decompiler, and you can see all references to INSTANCE in the bytecode are setting values correctly.



This is the actual output:





// ================Stackoverflow_53384931Kt.class =================
// class version 52.0 (52)
// access flags 0x31
public final class Stackoverflow_53384931Kt {


// access flags 0x1A
// signature Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
// declaration: kotlin.jvm.functions.Function0<kotlin.Unit>
private final static Lkotlin/jvm/functions/Function0; printKotlin
@Lorg/jetbrains/annotations/NotNull;() // invisible

// access flags 0x19
// signature ()Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
// declaration: kotlin.jvm.functions.Function0<kotlin.Unit> getPrintKotlin()
public final static getPrintKotlin()Lkotlin/jvm/functions/Function0;
@Lorg/jetbrains/annotations/NotNull;() // invisible
L0
LINENUMBER 3 L0
GETSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
ARETURN
L1
MAXSTACK = 1
MAXLOCALS = 0

// access flags 0x8
static <clinit>()V
L0
LINENUMBER 3 L0
GETSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
CHECKCAST kotlin/jvm/functions/Function0
PUTSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
RETURN
MAXSTACK = 1
MAXLOCALS = 0

@Lkotlin/Metadata; ...
// access flags 0x18
final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
// compiled from: stackoverflow-53384931.kt
}


// ================Stackoverflow_53384931Kt$printKotlin$1.class =================
// class version 52.0 (52)
// access flags 0x30
// signature Lkotlin/jvm/internal/Lambda;Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
// declaration: Stackoverflow_53384931Kt$printKotlin$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function0<kotlin.Unit>
final class Stackoverflow_53384931Kt$printKotlin$1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function0 {


// access flags 0x1041
public synthetic bridge invoke()Ljava/lang/Object;
ALOAD 0
INVOKEVIRTUAL Stackoverflow_53384931Kt$printKotlin$1.invoke ()V
GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
ARETURN
MAXSTACK = 1
MAXLOCALS = 1

// access flags 0x11
public final invoke()V
L0
LINENUMBER 4 L0
LDC "Hello Kotlin"
ASTORE 1
L1
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 1
INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
L2
L3
LINENUMBER 5 L3
RETURN
L4
LOCALVARIABLE this LStackoverflow_53384931Kt$printKotlin$1; L0 L4 0
MAXSTACK = 2
MAXLOCALS = 2

// access flags 0x0
<init>()V
ALOAD 0
ICONST_0
INVOKESPECIAL kotlin/jvm/internal/Lambda.<init> (I)V
RETURN
MAXSTACK = 2
MAXLOCALS = 1

// access flags 0x19
public final static LStackoverflow_53384931Kt$printKotlin$1; INSTANCE

// access flags 0x8
static <clinit>()V
NEW Stackoverflow_53384931Kt$printKotlin$1
DUP
INVOKESPECIAL Stackoverflow_53384931Kt$printKotlin$1.<init> ()V
PUTSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
RETURN
MAXSTACK = 2
MAXLOCALS = 0

@Lkotlin/Metadata; ...
OUTERCLASS Stackoverflow_53384931Kt null
// access flags 0x18
final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
// compiled from: stackoverflow-53384931.kt
// debug info: SMAP
...

}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384931%2fwhy-kotlin-decompiler-generates-null-instance%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    Use the Kotlin bytecode inspector to see what is being generated as JVM bytecode rather than trying to decompile bytecode to Java, which likely won't work for code generated by a different language compiler because it might not follow the expected patterns. A bytecode decompiler does not always generate working code.




    JVM bytecode != Java language specific



    Kotlin generated JVM bytecode != Java generated JVM bytecode




    The NULL instance you are seeing is a misinterpretation by your decompiler, and you can see all references to INSTANCE in the bytecode are setting values correctly.



    This is the actual output:





    // ================Stackoverflow_53384931Kt.class =================
    // class version 52.0 (52)
    // access flags 0x31
    public final class Stackoverflow_53384931Kt {


    // access flags 0x1A
    // signature Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
    // declaration: kotlin.jvm.functions.Function0<kotlin.Unit>
    private final static Lkotlin/jvm/functions/Function0; printKotlin
    @Lorg/jetbrains/annotations/NotNull;() // invisible

    // access flags 0x19
    // signature ()Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
    // declaration: kotlin.jvm.functions.Function0<kotlin.Unit> getPrintKotlin()
    public final static getPrintKotlin()Lkotlin/jvm/functions/Function0;
    @Lorg/jetbrains/annotations/NotNull;() // invisible
    L0
    LINENUMBER 3 L0
    GETSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
    ARETURN
    L1
    MAXSTACK = 1
    MAXLOCALS = 0

    // access flags 0x8
    static <clinit>()V
    L0
    LINENUMBER 3 L0
    GETSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
    CHECKCAST kotlin/jvm/functions/Function0
    PUTSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
    RETURN
    MAXSTACK = 1
    MAXLOCALS = 0

    @Lkotlin/Metadata; ...
    // access flags 0x18
    final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
    // compiled from: stackoverflow-53384931.kt
    }


    // ================Stackoverflow_53384931Kt$printKotlin$1.class =================
    // class version 52.0 (52)
    // access flags 0x30
    // signature Lkotlin/jvm/internal/Lambda;Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
    // declaration: Stackoverflow_53384931Kt$printKotlin$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function0<kotlin.Unit>
    final class Stackoverflow_53384931Kt$printKotlin$1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function0 {


    // access flags 0x1041
    public synthetic bridge invoke()Ljava/lang/Object;
    ALOAD 0
    INVOKEVIRTUAL Stackoverflow_53384931Kt$printKotlin$1.invoke ()V
    GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
    ARETURN
    MAXSTACK = 1
    MAXLOCALS = 1

    // access flags 0x11
    public final invoke()V
    L0
    LINENUMBER 4 L0
    LDC "Hello Kotlin"
    ASTORE 1
    L1
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    ALOAD 1
    INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
    L2
    L3
    LINENUMBER 5 L3
    RETURN
    L4
    LOCALVARIABLE this LStackoverflow_53384931Kt$printKotlin$1; L0 L4 0
    MAXSTACK = 2
    MAXLOCALS = 2

    // access flags 0x0
    <init>()V
    ALOAD 0
    ICONST_0
    INVOKESPECIAL kotlin/jvm/internal/Lambda.<init> (I)V
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 1

    // access flags 0x19
    public final static LStackoverflow_53384931Kt$printKotlin$1; INSTANCE

    // access flags 0x8
    static <clinit>()V
    NEW Stackoverflow_53384931Kt$printKotlin$1
    DUP
    INVOKESPECIAL Stackoverflow_53384931Kt$printKotlin$1.<init> ()V
    PUTSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 0

    @Lkotlin/Metadata; ...
    OUTERCLASS Stackoverflow_53384931Kt null
    // access flags 0x18
    final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
    // compiled from: stackoverflow-53384931.kt
    // debug info: SMAP
    ...

    }





    share|improve this answer




























      6














      Use the Kotlin bytecode inspector to see what is being generated as JVM bytecode rather than trying to decompile bytecode to Java, which likely won't work for code generated by a different language compiler because it might not follow the expected patterns. A bytecode decompiler does not always generate working code.




      JVM bytecode != Java language specific



      Kotlin generated JVM bytecode != Java generated JVM bytecode




      The NULL instance you are seeing is a misinterpretation by your decompiler, and you can see all references to INSTANCE in the bytecode are setting values correctly.



      This is the actual output:





      // ================Stackoverflow_53384931Kt.class =================
      // class version 52.0 (52)
      // access flags 0x31
      public final class Stackoverflow_53384931Kt {


      // access flags 0x1A
      // signature Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
      // declaration: kotlin.jvm.functions.Function0<kotlin.Unit>
      private final static Lkotlin/jvm/functions/Function0; printKotlin
      @Lorg/jetbrains/annotations/NotNull;() // invisible

      // access flags 0x19
      // signature ()Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
      // declaration: kotlin.jvm.functions.Function0<kotlin.Unit> getPrintKotlin()
      public final static getPrintKotlin()Lkotlin/jvm/functions/Function0;
      @Lorg/jetbrains/annotations/NotNull;() // invisible
      L0
      LINENUMBER 3 L0
      GETSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
      ARETURN
      L1
      MAXSTACK = 1
      MAXLOCALS = 0

      // access flags 0x8
      static <clinit>()V
      L0
      LINENUMBER 3 L0
      GETSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
      CHECKCAST kotlin/jvm/functions/Function0
      PUTSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
      RETURN
      MAXSTACK = 1
      MAXLOCALS = 0

      @Lkotlin/Metadata; ...
      // access flags 0x18
      final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
      // compiled from: stackoverflow-53384931.kt
      }


      // ================Stackoverflow_53384931Kt$printKotlin$1.class =================
      // class version 52.0 (52)
      // access flags 0x30
      // signature Lkotlin/jvm/internal/Lambda;Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
      // declaration: Stackoverflow_53384931Kt$printKotlin$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function0<kotlin.Unit>
      final class Stackoverflow_53384931Kt$printKotlin$1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function0 {


      // access flags 0x1041
      public synthetic bridge invoke()Ljava/lang/Object;
      ALOAD 0
      INVOKEVIRTUAL Stackoverflow_53384931Kt$printKotlin$1.invoke ()V
      GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
      ARETURN
      MAXSTACK = 1
      MAXLOCALS = 1

      // access flags 0x11
      public final invoke()V
      L0
      LINENUMBER 4 L0
      LDC "Hello Kotlin"
      ASTORE 1
      L1
      GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
      ALOAD 1
      INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
      L2
      L3
      LINENUMBER 5 L3
      RETURN
      L4
      LOCALVARIABLE this LStackoverflow_53384931Kt$printKotlin$1; L0 L4 0
      MAXSTACK = 2
      MAXLOCALS = 2

      // access flags 0x0
      <init>()V
      ALOAD 0
      ICONST_0
      INVOKESPECIAL kotlin/jvm/internal/Lambda.<init> (I)V
      RETURN
      MAXSTACK = 2
      MAXLOCALS = 1

      // access flags 0x19
      public final static LStackoverflow_53384931Kt$printKotlin$1; INSTANCE

      // access flags 0x8
      static <clinit>()V
      NEW Stackoverflow_53384931Kt$printKotlin$1
      DUP
      INVOKESPECIAL Stackoverflow_53384931Kt$printKotlin$1.<init> ()V
      PUTSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
      RETURN
      MAXSTACK = 2
      MAXLOCALS = 0

      @Lkotlin/Metadata; ...
      OUTERCLASS Stackoverflow_53384931Kt null
      // access flags 0x18
      final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
      // compiled from: stackoverflow-53384931.kt
      // debug info: SMAP
      ...

      }





      share|improve this answer


























        6












        6








        6







        Use the Kotlin bytecode inspector to see what is being generated as JVM bytecode rather than trying to decompile bytecode to Java, which likely won't work for code generated by a different language compiler because it might not follow the expected patterns. A bytecode decompiler does not always generate working code.




        JVM bytecode != Java language specific



        Kotlin generated JVM bytecode != Java generated JVM bytecode




        The NULL instance you are seeing is a misinterpretation by your decompiler, and you can see all references to INSTANCE in the bytecode are setting values correctly.



        This is the actual output:





        // ================Stackoverflow_53384931Kt.class =================
        // class version 52.0 (52)
        // access flags 0x31
        public final class Stackoverflow_53384931Kt {


        // access flags 0x1A
        // signature Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
        // declaration: kotlin.jvm.functions.Function0<kotlin.Unit>
        private final static Lkotlin/jvm/functions/Function0; printKotlin
        @Lorg/jetbrains/annotations/NotNull;() // invisible

        // access flags 0x19
        // signature ()Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
        // declaration: kotlin.jvm.functions.Function0<kotlin.Unit> getPrintKotlin()
        public final static getPrintKotlin()Lkotlin/jvm/functions/Function0;
        @Lorg/jetbrains/annotations/NotNull;() // invisible
        L0
        LINENUMBER 3 L0
        GETSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
        ARETURN
        L1
        MAXSTACK = 1
        MAXLOCALS = 0

        // access flags 0x8
        static <clinit>()V
        L0
        LINENUMBER 3 L0
        GETSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
        CHECKCAST kotlin/jvm/functions/Function0
        PUTSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
        RETURN
        MAXSTACK = 1
        MAXLOCALS = 0

        @Lkotlin/Metadata; ...
        // access flags 0x18
        final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
        // compiled from: stackoverflow-53384931.kt
        }


        // ================Stackoverflow_53384931Kt$printKotlin$1.class =================
        // class version 52.0 (52)
        // access flags 0x30
        // signature Lkotlin/jvm/internal/Lambda;Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
        // declaration: Stackoverflow_53384931Kt$printKotlin$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function0<kotlin.Unit>
        final class Stackoverflow_53384931Kt$printKotlin$1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function0 {


        // access flags 0x1041
        public synthetic bridge invoke()Ljava/lang/Object;
        ALOAD 0
        INVOKEVIRTUAL Stackoverflow_53384931Kt$printKotlin$1.invoke ()V
        GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
        ARETURN
        MAXSTACK = 1
        MAXLOCALS = 1

        // access flags 0x11
        public final invoke()V
        L0
        LINENUMBER 4 L0
        LDC "Hello Kotlin"
        ASTORE 1
        L1
        GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
        ALOAD 1
        INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
        L2
        L3
        LINENUMBER 5 L3
        RETURN
        L4
        LOCALVARIABLE this LStackoverflow_53384931Kt$printKotlin$1; L0 L4 0
        MAXSTACK = 2
        MAXLOCALS = 2

        // access flags 0x0
        <init>()V
        ALOAD 0
        ICONST_0
        INVOKESPECIAL kotlin/jvm/internal/Lambda.<init> (I)V
        RETURN
        MAXSTACK = 2
        MAXLOCALS = 1

        // access flags 0x19
        public final static LStackoverflow_53384931Kt$printKotlin$1; INSTANCE

        // access flags 0x8
        static <clinit>()V
        NEW Stackoverflow_53384931Kt$printKotlin$1
        DUP
        INVOKESPECIAL Stackoverflow_53384931Kt$printKotlin$1.<init> ()V
        PUTSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
        RETURN
        MAXSTACK = 2
        MAXLOCALS = 0

        @Lkotlin/Metadata; ...
        OUTERCLASS Stackoverflow_53384931Kt null
        // access flags 0x18
        final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
        // compiled from: stackoverflow-53384931.kt
        // debug info: SMAP
        ...

        }





        share|improve this answer













        Use the Kotlin bytecode inspector to see what is being generated as JVM bytecode rather than trying to decompile bytecode to Java, which likely won't work for code generated by a different language compiler because it might not follow the expected patterns. A bytecode decompiler does not always generate working code.




        JVM bytecode != Java language specific



        Kotlin generated JVM bytecode != Java generated JVM bytecode




        The NULL instance you are seeing is a misinterpretation by your decompiler, and you can see all references to INSTANCE in the bytecode are setting values correctly.



        This is the actual output:





        // ================Stackoverflow_53384931Kt.class =================
        // class version 52.0 (52)
        // access flags 0x31
        public final class Stackoverflow_53384931Kt {


        // access flags 0x1A
        // signature Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
        // declaration: kotlin.jvm.functions.Function0<kotlin.Unit>
        private final static Lkotlin/jvm/functions/Function0; printKotlin
        @Lorg/jetbrains/annotations/NotNull;() // invisible

        // access flags 0x19
        // signature ()Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
        // declaration: kotlin.jvm.functions.Function0<kotlin.Unit> getPrintKotlin()
        public final static getPrintKotlin()Lkotlin/jvm/functions/Function0;
        @Lorg/jetbrains/annotations/NotNull;() // invisible
        L0
        LINENUMBER 3 L0
        GETSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
        ARETURN
        L1
        MAXSTACK = 1
        MAXLOCALS = 0

        // access flags 0x8
        static <clinit>()V
        L0
        LINENUMBER 3 L0
        GETSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
        CHECKCAST kotlin/jvm/functions/Function0
        PUTSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
        RETURN
        MAXSTACK = 1
        MAXLOCALS = 0

        @Lkotlin/Metadata; ...
        // access flags 0x18
        final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
        // compiled from: stackoverflow-53384931.kt
        }


        // ================Stackoverflow_53384931Kt$printKotlin$1.class =================
        // class version 52.0 (52)
        // access flags 0x30
        // signature Lkotlin/jvm/internal/Lambda;Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
        // declaration: Stackoverflow_53384931Kt$printKotlin$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function0<kotlin.Unit>
        final class Stackoverflow_53384931Kt$printKotlin$1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function0 {


        // access flags 0x1041
        public synthetic bridge invoke()Ljava/lang/Object;
        ALOAD 0
        INVOKEVIRTUAL Stackoverflow_53384931Kt$printKotlin$1.invoke ()V
        GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
        ARETURN
        MAXSTACK = 1
        MAXLOCALS = 1

        // access flags 0x11
        public final invoke()V
        L0
        LINENUMBER 4 L0
        LDC "Hello Kotlin"
        ASTORE 1
        L1
        GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
        ALOAD 1
        INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
        L2
        L3
        LINENUMBER 5 L3
        RETURN
        L4
        LOCALVARIABLE this LStackoverflow_53384931Kt$printKotlin$1; L0 L4 0
        MAXSTACK = 2
        MAXLOCALS = 2

        // access flags 0x0
        <init>()V
        ALOAD 0
        ICONST_0
        INVOKESPECIAL kotlin/jvm/internal/Lambda.<init> (I)V
        RETURN
        MAXSTACK = 2
        MAXLOCALS = 1

        // access flags 0x19
        public final static LStackoverflow_53384931Kt$printKotlin$1; INSTANCE

        // access flags 0x8
        static <clinit>()V
        NEW Stackoverflow_53384931Kt$printKotlin$1
        DUP
        INVOKESPECIAL Stackoverflow_53384931Kt$printKotlin$1.<init> ()V
        PUTSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
        RETURN
        MAXSTACK = 2
        MAXLOCALS = 0

        @Lkotlin/Metadata; ...
        OUTERCLASS Stackoverflow_53384931Kt null
        // access flags 0x18
        final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
        // compiled from: stackoverflow-53384931.kt
        // debug info: SMAP
        ...

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 1:53









        Jayson MinardJayson Minard

        40.2k17112173




        40.2k17112173
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384931%2fwhy-kotlin-decompiler-generates-null-instance%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()