Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a Java static method on a Kotlin subclass?

Tags:

kotlin

I've created a Kotlin subclass of a Java class:

class AlarmReceiver : WakefulBroadcastReceiver() {

    companion object {
        const val ACTION_NOTIFY = "..."
    }

    override fun onReceive(context: Context, intent: Intent) { ... }
}

WakefulBroadcastReceiver has two static methods:

  • static boolean completeWakefulIntent(Intent intent)
  • static ComponentName startWakefulService(Context context, Intent intent)

and calling these from within my AlarmReceiver class works just as I expect. However, I'd like to call one of these methods outside of my Kotlin subclass.

The Problem

If I try AlarmReceiver.completeWakefulIntent(intent) from a different Kotlin class, I get the following compilation error:

Unresolved reference: completeWakefulIntent

I think this is because the compiler is trying to resolve the method on AlarmReceiver's companion object instead of finding the inherited method from its superclass. As a workaround, I can directly define a method with the same signature on AlarmReceiver.Companion:

class AlarmReceiver : WakefulBroadcastReceiver() {

    companion object {
        const val ACTION_NOTIFY = "..."

        // Just call the superclass implementation for now
        fun completeWakefulIntent(intent: Intent): Boolean =
            WakefulBroadcastReceiver.completeWakefulIntent(intent)
    }

    ...
}

I think this has the same behavior I would have gotten had I relied on the default inherited method from a Java subclass, but I'm wondering if there's a better way to do this.

Is there a way to call an inherited static Java method on a Kotlin subclass?

like image 489
Ronald Martin Avatar asked Aug 26 '16 17:08

Ronald Martin


People also ask

How do you call a static method from a subclass?

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.

Can static method be accessed by subclass?

Static methods in Java are inherited, but can not be overridden. If you declare the same method in a subclass, you hide the superclass method instead of overriding it. Static methods are not polymorphic. At the compile time, the static method will be statically linked.

How do you call a static method in Kotlin?

In order to implement a static method in Kotlin, we will take the help of "companion objects". Companion objects are the singleton objects whose properties and functions are tied to a class but not to the instance of that class. Hence, we can access them just like a static method of the class.

What is the equivalent of Java static methods in Kotlin?

By using @JvmStatic annotation Adding @JvmStatic annotation simply before the members to be declared as the equivalent of static ones in Java works well in Kotlin to provide the same functionality.


1 Answers

In Kotlin, unlike Java, static members are not inherited by subclasses, even though they can be called inside a subclass without the base class name.

Outside a subclass, you have to call the base class static functions using the base class name:

WakefulBroadcastReceiver.completeWakefulIntent(intent)

This behavior seems to lie within the companion objects concept: companion objects of classes in a hierarchy are not included into each other.

Interestingly, for interfaces the situation is a bit different: interface static members cannot be referenced in subclasses without the interface name. This behavior is the same to that in Java.

like image 179
hotkey Avatar answered Sep 18 '22 11:09

hotkey