Faced with unexpected problem. Here is simple class:
class PaymentPrintTest {
init {
prepareToPrint()
}
private var sale: Int? = null
private var saleContent: ArrayList<Int> = ArrayList()
private fun prepareToPrint() {
sale = 5
saleContent = arrayListOf(1,2,3)
Log.i("WhereIsContent?", "prepare sale: $sale")
Log.i("WhereIsContent?", "prepare saleContent: ${saleContent.size}")
}
fun startPrint() {
Log.i("WhereIsContent?", "start sale: $sale")
Log.i("WhereIsContent?", "start saleContent: ${saleContent.size}")
}
}
This way I call method startPrint:
PaymentPrintTest().startPrint()
In logcat I see:
I/WhereIsContent?: prepare sale: 5
I/WhereIsContent?: prepare saleContent: 3
I/WhereIsContent?: start sale: 5
I/WhereIsContent?: start saleContent: 0
The question is where the content of saleContent value has gone? And why 'sale' variable has correct value at the same time?
saleContentalateinit varand remove the initialization in that line:private lateinit var saleContent: ArrayList<Int>. The remaining code can stay as it is.