# Calling a secondary constructor from a primary constructor

**URL:** <https://discuss.kotlinlang.org/t/calling-a-secondary-constructor-from-a-primary-constructor/846>\
**Category:** Uncategorized\
**Created:** [April 22, 2013, 3:25pm UTC](https://discuss.kotlinlang.org/t/calling-a-secondary-constructor-from-a-primary-constructor/846 "2013-04-22T15:25:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [April 22, 2013, 3:25pm UTC](https://discuss.kotlinlang.org/t/calling-a-secondary-constructor-from-a-primary-constructor/846/1 "2013-04-22T15:25:03Z")

</div>

Hi All,

I’ve two questions:

1 - How to define a secondary constructor in Kotlin ?

2 - Is it possible to call a SECONDARY constructor from a PRIMARY constructor ?

---

<div class="post-metadata">

**Author:** ![B7W](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@B7W](https://discuss.kotlinlang.org/u/B7W)\
**Post date:** [April 22, 2013, 4:20pm UTC](https://discuss.kotlinlang.org/t/calling-a-secondary-constructor-from-a-primary-constructor/846/2 "2013-04-22T16:20:38Z")

</div>

In Kotlin only primery constructor.

But you can do smth like this

```kotlin

class Greeter(val name : String) {
  fun greet() {
  println(“Hello, ${name}”);
  }
}
fun Greeter(name: Int) = Greeter(name.toString())   
fun main(args : Array<String>) {
  Greeter(1).greet()
}

```

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [April 24, 2013, 6:03pm UTC](https://discuss.kotlinlang.org/t/calling-a-secondary-constructor-from-a-primary-constructor/846/3 "2013-04-24T18:03:37Z")

</div>

Probably there is a good explanation for it, but I don't get the motivation, for having only a single constructor.

In your example, instead of having a public function out of class to initiate it, wouldn’t it be good to have it inside and as a Construtor ?

```kotlin
class Greeter(val name: String) {
    fun Greeter(name: Int) {
       supert(name.toString)
    }
    fun greet() {
        /// ....
    }
}

```

---

<div class="post-metadata">

**Author:** ![B7W](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@B7W](https://discuss.kotlinlang.org/u/B7W)\
**Post date:** [April 24, 2013, 6:29pm UTC](https://discuss.kotlinlang.org/t/calling-a-secondary-constructor-from-a-primary-constructor/846/4 "2013-04-24T18:29:57Z")

</div>

Because if we have default arguments, you can cover most cases in single constructor.

I think other constructors are only sugar. They disappointment me some times. It is better to use factory methods for this purpose.  
The advantage of using the ordinary function to emulate constructor is possibility to add it in any library without changing code. I think it is cool.
