Best way to declare member variable in Activity

Noobie to Kotlin here, so this may be a silly question…

I have an Android Activity that is passed a parameter “XYZ” on launch.

In the onCreate method of the Activity, I want to initialise a Member variable of the activity, passing the XYZ parameter to the constructor.

What is the best way to declare this member variable.

Kotlin doesn’t allow me to assign null to it, and I REALLY don’t want to make this member variable nullable. One of the reasons I like Kotlin is the fact that I can create classes that can’t ever be null.

How can I declare the member variable in the knowledge that it can’t be constructed until the onCreate of the Activity.

1 Like


Ah… lateinit, solves my problem and very handy indeed.

Thanks for the response. I felt a bit foolish asking the question, but I figured other people might have the same issue.

1 Like

nice , Here i found the example in kotlin
[](http://Declare a Android Global Variable in Kotlin)