2

As we know to declare a variable static , we have to use companion object.

A simple Example is listed below.

class MainActivity : AppCompatActivity() {

    companion object {
        val extraMessage = "message"

    }
}

This can be accessed in Other Activity as MainActivity.extraMessage , pretty neat and simple.

However the document states , the syntax looks like its static but at runtime those are still instance members of real object.

So is it like Kotlin does not have static members or Kotlin has just provided a simpler way to write the code.

2 Answers 2

2

In intelliJ you can have the kotlin plugin decompile the generated bytecode so you can see what is going on. Your code generates an approximation of the following code:

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import org.jetbrains.annotations.NotNull;
import android.support.v7.app.AppCompatActivity;

@Metadata(
   mv = {1, 1, 7},
   bv = {1, 0, 2},
   k = 1,
   d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\u0018\u0000 \u00032\u00020\u0001:\u0001\u0003B\u0005¢\u0006\u0002\u0010\u0002¨\u0006\u0004"},
   d2 = {"LMainActivity;", "Landroid/support/v7/app/AppCompatActivity;", "()V", "Companion", "special module for files not under source root"}
)
public final class MainActivity extends AppCompatActivity {
   @NotNull
   private static final String extraMessage = "message";
   public static final MainActivity.Companion Companion = new MainActivity.Companion((DefaultConstructorMarker)null);

   @Metadata(
      mv = {1, 1, 7},
      bv = {1, 0, 2},
      k = 1,
      d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u0014\u0010\u0003\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006¨\u0006\u0007"},
      d2 = {"LMainActivity$Companion;", "", "()V", "extraMessage", "", "getExtraMessage", "()Ljava/lang/String;", "special module for files not under source root"}
   )
   public static final class Companion {
      @NotNull
      public final String getExtraMessage() {
         return MainActivity.extraMessage;
      }

      private Companion() {
      }

      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

For the purposes of this answer I have removed the extension to allow it to compile in plain Kotlin, it should not affect the answer

This code:

class MainActivity {
    companion object {
        val extraMessage = "message"
    }
}

gives this java code when the class file is decompiled (using http://www.javadecompilers.com/):

import kotlin.Metadata;

public final class MainActivity {
    public static final MainActivity.Companion Companion = new MainActivity.Companion(null);
    private static final String extraMessage = "message";

    public MainActivity() {}
}

As this shows, the extraMessage is stored as a static field of the generated class, and Kotlin allows the field to be accessed the same as it would in Java.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.