Foggy day

Kotlin - companion object 본문

Kotlin

Kotlin - companion object

jinhan38 2021. 3. 16. 21:32

Kotlin don't have static. But it have companion object.

 

class KotlinPlayGroundActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_play_ground)

        Person.create("companion object text")
    }

    data class Person(val name: String, val registered: Instant = Instant.now()) {

        companion object {
            fun create(test: String): Person {
                println("test : $test")
                TODO("testing...")
            }
        }
    }
}

 

 

If you add TODO(), it throws an exception.

'Kotlin' 카테고리의 다른 글

Kotlin - collection about immutable, mutable  (0) 2021.03.16
Kotlin - Singleton  (0) 2021.03.16
Kotlin - destructing  (0) 2021.03.16
Kotlin - data class  (0) 2021.03.15
Kotlin - class extension  (0) 2021.03.15