Short question
Can I modify the visibility of a Kotlin object's INSTANCE
(for Java interop) to internal
or lower?
Long question
I'm writing a library and I want to have an API file / class, written in Kotlin, that exposes a function to be called from either Java or Kotlin like this:
Kotlin:
API.function()
Java:
API.function();
I can achieve this by writing it like this:
Kotlin:
object API {
@JvmStatic
fun function() = TODO()
}
However, now I can also do this:
Java:
API.INSTANCE.function();
I want to prevent this access to INSTANCE
to keep my API surface to a minimum for simplicity.
Can I modify the visibility of INSTANCE
to internal
or lower?
It's probably not possible, because any call to API
(from Kotlin) returns the object's instance and that should probably be hidden too for this to be possible. However, I'm curious to see if it is without major hacks.
A solution using Java would be to write API
in Java:
public final class API {
private API() {
}
public static void function() {
}
}
However, I'm looking for a solution written in Kotlin.