Created
January 27, 2018 07:43
-
-
Save abdurahmanadilovic/b6daae960d8c14fbda3bdad8ef174e2e to your computer and use it in GitHub Desktop.
Kotlin class order or execution
This file contains hidden or 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
class OrderOfExecution(name: String) { | |
val firstProperty = "First property: $name".also(::println) | |
init { | |
println("First initializer block that prints ${name}") | |
} | |
val secondProperty = "Second property: ${name.length}".also(::println) | |
init { | |
println("Second initializer block that prints ${name.length}") | |
} | |
constructor(order: Int): this(order.toString()){ | |
println("A secondary constructor") | |
} | |
} | |
fun main(args: Array<String>) { | |
OrderOfExecution("Abdurahman") | |
OrderOfExecution(12) | |
} | |
// ---output for OrderOfExecution("Abdurahman")--- | |
// First property: Abdurahman | |
// First initializer block that prints Abdurahman | |
// Second property: 10 | |
// Second initializer block that prints 10 | |
// ---output for OrderOfExecution(12)--- | |
// First property: 12 | |
// First initializer block that prints 12 | |
// Second property: 2 | |
// Second initializer block that prints 2 | |
// A secondary constructor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment