Kotlin Android - Static method in Kotlin - Companion object example

This example contains example with follwowing points:
1) Create static method in kotlin - kotlin companion object
2) How to use companion class to call method from within Java code
3) How to use companion class to call method from within Kotlin code
4) @JvmStatic - You can either add a @JvmStatic annotation or name your companion class to call method from within Java code
5) Pass MainActivity context to static method and use it to toast message in static function of kotlin companion object.

MainActivity.kt
package com.example.espl.staticmethodkotlin

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class MainActivity : AppCompatActivity() {
//Equivalent of Java static methods in Kotlin is kotlin Companion object
/**
 The java code for the static function is like this:
      public static int foo(Context context) {
            Toast.makeText(context, "Called foo", Toast.LENGTH_SHORT).show();
      }
 **/

//    create static method in kotlin
//kotlin companion object
companion object {
        @JvmStatic
        fun foo(context: Context,string: String) {
//            toast("Hello World!");
            Toast.makeText(context, "Called foo from: "+string, Toast.LENGTH_SHORT).show();
        }

    fun bar(context: Context,string: String) {
        Toast.makeText(context, "Called bar from: "+string, Toast.LENGTH_SHORT).show();
        }
}

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Use foo() method from inside Kotlin code
        MainActivity.foo(this,"MainActivity");

       //use it from inside Kotlin code
        MainActivity.bar(this,"MainActivity");

        val testobject: TestJavaClass= TestJavaClass(this)
        testobject.callMehodInJavaClass()
    }
}

TestJavaClass.java
package com.example.espl.staticmethodkotlin;
import android.content.Context;

public class TestJavaClass {
    Context mContext;
    public TestJavaClass(Context context) {
        mContext=context;
    }

void callMehodInJavaClass(){
//    You can either add a @JvmStatic annotation or name your companion class to call method from within Java code

   MainActivity.Companion.bar(mContext,"TestJavaClass-callMehodInJavaClass"); //works
//    If you don't like having to specify the Companion like above, then need to add a @JvmStatic            annotation in method decleration

   MainActivity.foo(mContext,"TestJavaClass-callMehodInJavaClass");  //  @JvmStatic

//    MainActivity.bar(mContext);
//    This function not work as  @JvmStatic is not added in the method/function bar() decleration

//    MainActivity.Companion.foo(mContext,"TestJavaClass-callMehodInJavaClass");
//     Above method works if  @JvmStatic is not added before the method/function foo() decleration, need to specify the Companion as above
    }
}


No comments:

Post a Comment

Popular Posts