Why class function print() prints empty list?

Hello, I am new to this forum. And I am also new to Kotlin. I don’t understand why the print function prints an empty list? For all I know, intList should have class scope, not function scope. Thank you for the answer.

class addIntList() {
    val intList = mutableListOf<Int>()
    fun addInt(newInt: Int):addIntList {
        intList.add(newInt)
        print(intList) //this does not print empty list
        return this
	}
    
    fun print() {
        print(intList)//this prints empty list
    }
}

addIntList().addInt(1).addInt(2)
addIntList().print()
1 Like
  1. Use the code style, please. Class names start with a capital letter.
  2. In the second line, you create a new instance of the class. It is obvious it does not know anything about the first instance.
2 Likes

Oh OK, thanks for your answer. May I ask you one more?
Why this produces an error: Unresolved reference: intList ? Shouldn’t intList became global scope? Thank you for the answer.

class AddIntList() {
    fun addInt(newInt: Int):AddIntList {
        intList.add(newInt) //error: Unresolved reference: intList

        print(intList) //error: Unresolved reference: intList

        return this
	}
    
    fun print() {
        print(intList)  //error: Unresolved reference: intList

    }
}

fun main() {
     val intList = mutableListOf<Int>()
    
     AddIntList().addInt(1).addInt(2)
     AddIntList().print()
}
1 Like

intList is declared in main and visible only within it. If you move the declaration outside main it will be a global for real. (don’t do that globals are evil).

Here you’re declaring 2 objects.
It’s hard to tell what you’re trying to do now, since these classes don’t have any state but this code looks very suspicious.

Since the class AddIntList doesn’t have any state (any val/var argument passed to the constuctor or val/var fields) there’s no point in it being a class, one would usually just use 2 functions.

1 Like

Oh OK, thank you for your answer. I am confused because I have seen val declared in main and works in a class called from within main(). But thank you anyway.

1 Like

I’m not sure what you trying to do, but I’ll break your code and try to explain what happens.

class AddIntList() {
    val intList = mutableListOf<Int>() // this is called instance variable and visible only inside single instance

    fun addInt(newInt: Int):addIntList {
        intList.add(newInt)
        return this
    }
    
    fun print() {
        print(intList)
    }
}

fun main() {
    AddIntList()  // new instance of AddIntList is created, intList is initialised as an empty list
        .addInt(1) // add new item to the list
        .addInt(2) // add another item to the list
        .print()      // prints the list with 2 items

    AddIntList() // new instance of AddIntList is created, NOTE: intList is a new empty list
        .print()     // since no items was added to the list this prints an empty list

    // usage with val
    val myList = AddIntList() // new instance of AddIntList, intList inside it is an empty list
    myList.print() // this prints an empty list
    myList.addInt(1).addInt(2).addInt(3) // add 3 items to the (internal) intList
    myList.print() // prints the list with 3 items
}

Thank you for your answer.