develop with

Static Blocks

Static blocks and companions objects in kotlin, variables, methods, class

When you need an initialization of a block of code when a class in Kotlin is created, you need to create a companion block. The companion block is the equivalent to a static block in Java.

An example of a companion block / static:

class MyClass {

     companion object {
        init {
        
        }
    }
}

For more details on companion blocks go here.

Next: Annotations

comments powered by Disqus

Want to see a topic covered? create a suggestion

Get more developer references and books in the developwith store.