• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - How to check if a lateinit variable has been initialized or not?

Answer -

You can check if the lateinit variable has been initialized or not before using it with the help of isInitialized method. This method will return true if the lateinit property has been initialized otherwise it will return false. For example:

class Person {
 lateinit var name: String
 fun initializeName() {
 println(this::name.isInitialized)
 name = "MindOrks" // initializing name
 println(this::name.isInitialized)
 }
}
fun main(args: Array) {
 Person().initializeName()
}

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners