How to pass a reference to an instance

Hi,

I am stuck at doing something very simple. I need to pass a reference to class, preferably at class constructor, but other means can be made to work too. Singleton does not work in following example.
Like so:

fun main(args: Array<String>) {
    class Logger() {
        var hours : Int = 0
    }
    class Simulate(val loggerRef : Logger) {
        fun doSomething() {
            loggerRef.hours += 1
        }
    }
    val log1 = Logger()
    val log2 = Logger()
    val sim1 = Simulate(log1)
    val sim2 = Simulate(log2)
    sim1.doSomething()
    sim2.doSomething()
    sim2.doSomething()
    println(log1.hours) //This should be 1
    println(log2.hours) //This should be 2
}

ps. corected wrong syntax

That code runs correctly, if you fix the mistakes. “Class” should be “class”; “fun dosomething” should be “doSomething”; and log1, log2, sim1 and sim2 need to be declared as val or var, e.g. “val log1 = Logger()”.

I corrected the code and tested, it works, how is that possible? I thought kotlin does not pass references (objects will get “new” instantiation upon passing them), but apparently i was mistaken.

I couldn’t find an explanation in the Kotlin reference documentation, but see this: Function parameters are "val" not "var"?

Kotlin passes objects by reference and not by value. The exception to this are primitive types eg. Int, Float, Boolean, etc. I can’t find the place in the docs where this is described though.

Thank you for the clarification. I wish there were good books available on Kotlin (i have found one).

I got perhaps mixed up with Kotlin “Object”, which is a singleton. But in Java objects are the class instances.