Last active
January 1, 2022 14:26
-
-
Save arsalankhan994/ddd4e8dcd854bb26c57fcecd77f470a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
/* | |
Simple object initialization | |
*/ | |
val objectInitialization = ObjectInitialization() | |
objectInitialization.printMyName("Erselan Khan") | |
objectInitialization.addTwoNumbers(firstNumber = 2, secondNumber = 2) | |
} | |
class ObjectInitialization { | |
/* | |
Must define the type of lateinit i.e "Number" | |
*/ | |
lateinit var initializationLateInitObject: Number | |
fun printMyName(name: String) { | |
println("My name is $name") | |
} | |
/* | |
initializing lateinit variable | |
*/ | |
fun addTwoNumbers(firstNumber: Int, secondNumber: Int) { | |
initializationLateInitObject = firstNumber + secondNumber | |
printLateInitVariableValue() | |
} | |
/* | |
accessing lateinit variable after checking it's initialization | |
*/ | |
fun printLateInitVariableValue() { | |
if (this::initializationLateInitObject.isInitialized) { | |
println("Print value: ${initializationLateInitObject.toInt()}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment