๊ฐœ๋ฐœ/Kotlin

Kotlin constructor ์ •๋ฆฌ

๋„๋ฆฌ ๐ŸŸ 2021. 4. 8. 23:47

์ž‘์„ฑ์ผ: 2019.12.31


Kotlin์˜ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ Primary constructor(์ฃผ ์ƒ์„ฑ์ž)์™€ ํ•˜๋‚˜ ์ด์ƒ์˜ Secondary constructor(๋ถ€ ์ƒ์„ฑ์ž)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Primary constructor

Primary constructor๋Š” ํด๋ž˜์Šค ์ด๋ฆ„ ๋’ค์— ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

class Person constructor(firstName: String) { /*...*/ }

Primary constructor์— ์–ด๋…ธํ…Œ์ด์…˜์ด๋‚˜ ์ ‘๊ทผ ์ œํ•œ์ž(visibility modifiers)๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, constructor ํ‚ค์›Œ๋“œ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

class Person(firstName: String) { /*...*/ }

Primary constructor๋Š” ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ๊ฐ€์งˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋Š” init ๋ธ”๋Ÿญ ์•ˆ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

class Person(firstName: String) { 
  private val nameLength = firstName.length
    	
  init {
    println("Second initializer block that prints $nameLength")
  }
}

ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”์™€ init ๋ธ”๋Ÿญ์˜ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋Š” ์ž‘์„ฑํ•œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

class InitOrderDemo(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}")
    }
}

> ์‹คํ–‰ ๊ฒฐ๊ณผ

First property: hello
First initializer block that prints hello
Second property: 5
Second initializer block that prints 5

Primary constructor์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”์™€ init ๋ธ”๋Ÿญ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Customer(name: String) {
    val customerKey = name.toUpperCase()
}

์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด, Primary constructor ์•ˆ์—์„œ ํ”„๋กœํผํ‹ฐ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ํ•œ๋ฒˆ์— ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Primary constructor ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ var๋˜๋Š” val๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

class Person(val firstName: String, val lastName: String, var age: Int) { /*...*/ }

์ƒ์„ฑ์ž์— ์–ด๋…ธํ…Œ์ด์…˜์ด๋‚˜ ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค๋ฉด constructor ํ‚ค์›Œ๋“œ๋ฅผ ๊ผญ ๋ถ™์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

class Customer public @Inject constructor(name: String) { /*...*/ }

Secondary constructor

constructor ํ‚ค์›Œ๋“œ๋กœ Secondary constructor๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Person {
    var children: MutableList<Person> = mutableListOf<Person>();
    constructor(parent: Person) {
        parent.children.add(this)
    }
}

ํด๋ž˜์Šค์— Primary constructor๋„ ์žˆ๋‹ค๋ฉด, Secondary constructor์—์„œ Primary constructor๋กœ ์ง์ ‘ ์œ„์ž„ ๋˜๋Š” ๋‹ค๋ฅธ Secondary constructor๋ฅผ ํ†ตํ•œ ๊ฐ„์ ‘ ์œ„์ž„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํด๋ž˜์Šค ์•ˆ์— ์žˆ๋Š” ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋กœ์˜ ์œ„์ž„์€ this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

class Person(val name: String) {
    var children: MutableList<Person> = mutableListOf<Person>();
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋Š” Primary constructor์˜ ์ผ๋ถ€๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์—, Secondary constructor ์‹คํ–‰ ์ด์ „์— ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ์™€ init ๋ธ”๋Ÿญ์ด ๋จผ์ € ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. Primary constructor๊ฐ€ ์—†๋”๋ผ๋„ ์•”์‹œ์ ์œผ๋กœ ์œ„์ž„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ์ด Secondary constructor๋ณด๋‹ค ํ•ญ์ƒ ๋จผ์ € ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

class Constructors {
    init {
        println("Init block")
    }
    
    constructor(i: Int) {
        println("Constructor")
    }
}

> ์‹คํ–‰ ๊ฒฐ๊ณผ

Init block
Constructor

์ถ”์ƒ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค๋Š” ๋ณ„๋„๋กœ ์ƒ์„ฑ์ž ์„ ์–ธ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด, ์ธ์ž๊ฐ€ ์—†๋Š” public, primary constructor๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ public ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€๋ฉด ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ, ๋นˆ private constructor๋ฅผ ์„ ์–ธํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

class DontCreateMe private constructor () { /*...*/ }

 

NOTE: Primary constructor์˜ ๋ชจ๋“  ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋””ํดํŠธ ๊ฐ’์„ ๊ฐ–๋Š” ๊ฒฝ์šฐ, JVM ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ์ƒ์„ฑ์ž์—์„œ๋Š” ์„ ์–ธ๋œ ๋””ํดํŠธ ๊ฐ’์œผ๋กœ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋นˆ ์ƒ์„ฑ์ž๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” Jackson์ด๋‚˜ JPA๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ Kotlin์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

References