How to access instance variable in static companion object in Kotlin
A companion object is not part of an instance of a class. You can't access members from a companion object, just like in Java you can't access members from a static method.
Instead, don't use a companion object:
class AsyncService(val command: Command, val context: Context) {
fun doGet(request: String) {
doAsync {
val jsonObj = java.net.URL(request).readText()
command.execute(JSONObject(jsonObj))
}
}
}
You should pass arguments directly to your companion object function:
class AsyncService {
companion object {
fun doGet(command: Command, context: Context, request: String) {
doAsync {
val jsonObj = java.net.URL(request).readText()
command.execute(JSONObject(jsonObj))
}
}
}
}