Hello everyone! How to use property from object? C...
# getting-started
s
Hello everyone! How to use property from object? Code in 🧵
👋 1
I have
Copy code
object ExampleObject {
    
    val objectInObject = object {
        val number = 1
    }
}
and I cannot get number. In example this not working
Copy code
val test = ExampleObject.objectInObject.number
a
Not sure this can help I created this object
Copy code
class GlobalVals {
    companion object {
        var numOfTimes: Int = 0
        fun incNumOfTimes(){
            numOfTimes = numOfTimes.inc()
        }
    }
}
Then i used them like this in main()
Copy code
GlobalVals.incNumOfTimes()
s
Thanks, @Akshat Sharma! But this isn't the solution I'm looking for.
c
You can do:
Copy code
object ExampleObject {
  object ObjectInObject {
    val number = 1
  }
}
Why do you want to put objects inside objects?
👍 1
What's the error in your version?
s
Yes, that's it! Thanks, @CLOVIS. I do this for nested navigation in my app
s
.
🚫 1
c
I don't think you can use () on an object, they are initialized implicitly
d
@Sanendak In your original example you've created property
objectInObject
which was initiliazed by anonymous object. Since Anonymous object doesn't have any denotable name (actually it's name is
ExampleObject$objectInObject$1
), then compiler approximates type of
objectInObject
to its supertype (
Any
in this case), which obviously doesn't have property
number
c
I highly recommend you enable inlay type hints, that will make these cases much, much easier to understand @Sanendak: https://www.jetbrains.com/help/idea/inlay-hints.html
1