Accessing Kotlin Sealed Class from Java

ssawchenko :

Up until now I have been using this Kotlin sealed class:

sealed class ScanAction {
   class Continue: ScanAction()
   class Stop: ScanAction()
   ... /* There's more but that's not super important */
}

Which has been working great in both my Kotlin and Java code. Today I tried changing this class to use objects instead (as is recommended to reduce extra class instantiation):

sealed class ScanAction {
   object Continue: ScanAction()
   object Stop: ScanAction()
}

I am able to reference this easy peasy in my other Kotlin files, but I am now struggling to use it in my Java files.

I have tried the following and both of these kick back compilation errors when trying to make reference to in Java:

ScanAction test = ScanAction.Continue;

ScanAction test = new ScanAction.Continue();

Does anyone know how I can reference the instance in Java now?

Francesc :

You have to use the INSTANCE property:

ScanAction test = ScanAction.Continue.INSTANCE;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36777&siteId=1