Home > Software engineering >  When will the init{...} be launched in a Class of Kotlin?
When will the init{...} be launched in a Class of Kotlin?

Time:08-10

I run Code A, and get the error Result A. It seems that isRecordingState has't been initialized.

So I modify Code A as Code B, and Code B can run correctly.

In my mind, I can place different functions in any order in a class of Kotlin.

I think init{ } of a Class will be launched after the object has been initialized, so I think I can place init{ } in any place of a class.

What's wrong with my ideas?

Code A

@HiltViewModel
class SoundViewModel @Inject constructor(
    @ApplicationContext private val appContext: Context,
    ...
): ViewModel() {

    init { beginSoundDensity() }   

    private val _timeXState = MutableStateFlow(0)
    
    var isRecordingState by mutableStateOf(false)
        private set

    private var myJob: Job?=null


    fun beginSoundDensity() {
        if (isRecordingState == false) {
            isRecordingState = true

            myJob?.cancel()
            ...
           
        }
    }
}

Result A

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object androidx.compose.runtime.State.getValue()' on a null object reference
        at info.dodata.soundmeter.presentation.viewmodel.SoundViewModel.isRecordingState(SoundViewModel.kt:245)
        at info.dodata.soundmeter.presentation.viewmodel.SoundViewModel.beginSoundDensity(SoundViewModel.kt:81)
        at info.dodata.soundmeter.presentation.viewmodel.SoundViewModel.<init>(SoundViewModel.kt:39)

Code B

@HiltViewModel
class SoundViewModel @Inject constructor(
    @ApplicationContext private val appContext: Context,
    ...
): ViewModel() {

    private val _timeXState = MutableStateFlow(0)

    var isRecordingState by mutableStateOf(false)
        private set

    private var myJob: Job?=null
    
    init { beginSoundDensity() }

    fun beginSoundDensity() {
        if (isRecordingState == false) {
            isRecordingState = true

            myJob?.cancel()
            ...

        }
    }
}

CodePudding user response:

The code just runs from top to bottom, so this code for example prints "12345"

fun main() {
    A()
}

class A {
    init {
        print("1")
    }
    val s2 = printAndReturn("2")
    init {
        print("3")
    }
    val s4 = printAndReturn("4")
    init {
        print("5")
    }
    private fun printAndReturn(s: String): String {
        print(s)
        return s
    }
}
  • Related