Skip to content

Instantly share code, notes, and snippets.

@abdurahmanadilovic
Created January 27, 2018 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdurahmanadilovic/b6daae960d8c14fbda3bdad8ef174e2e to your computer and use it in GitHub Desktop.
Save abdurahmanadilovic/b6daae960d8c14fbda3bdad8ef174e2e to your computer and use it in GitHub Desktop.
Kotlin class order or execution
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