Import Boolean from another class


#1

Hi
Before: in java I had 2 Booleans directly under the main class of main activity, and they were imported to the preference activity
after converting to Kotlin
they went under companion object

//Main activity
class MainActivity : AppCompatActivity() {

    companion object {
        var bOne: Boolean? = true
        var bTwo: Boolean? = false
    }
...
}

//Preference activity
import jcalc.app.nhk.MainActivity.bOne
import jcalc.app.nhk.MainActivity.bTwo

they work in the main activity but their import doesn’t work anymore in the preferences activity class

when under companion object: Error Unresolved reference
when under the class directly: Error Cannot Import 'bOne', functions and properties can be imported only from packages or objects

going under companion object is legit but what should i do to fix the Unresolved reference error? sorry i’m new to kotlin


#2

From kotlin try:

import jcalc.app.nhk.MainActivity.Companion.bOne

and from java, if needed, you would omit the “Companion”

import jcalc.app.nhk.MainActivity.bOne

#3

i found that the problem was just that the automatically created companion object didn’t have a specific name to import to another place, so i called it companion (companion object companion{}) and everything worked

thanks for help


#4

The name of the companion object can be omitted, in which case the name Companion will be used

https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects