# Difference between primary and secondary

**URL:** <https://discuss.kotlinlang.org/t/difference-between-primary-and-secondary/3277>\
**Category:** Uncategorized\
**Created:** [June 13, 2017, 6:45am UTC](https://discuss.kotlinlang.org/t/difference-between-primary-and-secondary/3277 "2017-06-13T06:45:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![almasisz](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@almasisz](https://discuss.kotlinlang.org/u/almasisz)\
**Post date:** [June 13, 2017, 6:45am UTC](https://discuss.kotlinlang.org/t/difference-between-primary-and-secondary/3277/1 "2017-06-13T06:45:58Z")

</div>

Can someone explain the differences between primary and secondary constructor? Except that they are written in difference places. Thank you

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [June 13, 2017, 6:53am UTC](https://discuss.kotlinlang.org/t/difference-between-primary-and-secondary/3277/2 "2017-06-13T06:53:33Z")

</div>

Not so big differences

> A class in Kotlin can have a primary constructor and one or more secondary constructors.

> The primary constructor cannot contain any code.

> Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body

> for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax

> If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor

[http://kotlinlang.org/docs/reference/classes.html](http://kotlinlang.org/docs/reference/classes.html)

A major difference is for data classes

> The compiler automatically derives the following members from all properties declared in the primary constructor: …

[http://kotlinlang.org/docs/reference/data-classes.html](http://kotlinlang.org/docs/reference/data-classes.html)

---

<div class="post-metadata">

**Author:** ![almasisz](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@almasisz](https://discuss.kotlinlang.org/u/almasisz)\
**Post date:** [June 13, 2017, 7:17am UTC](https://discuss.kotlinlang.org/t/difference-between-primary-and-secondary/3277/3 "2017-06-13T07:17:48Z")

</div>

Thank you for the answer. The strange thing for me was that, i did not find any difference if i write a primary constructor with an initialization block and logic in it, or only a secondary constructor with the same logic in it.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [June 13, 2017, 7:35am UTC](https://discuss.kotlinlang.org/t/difference-between-primary-and-secondary/3277/4 "2017-06-13T07:35:53Z")

</div>

You can declare properties in primary constructor, you can define logic in secondary constructor,  
and finally you can spread initialization logic through the class using `init`.

> **[An in-depth look at Kotlin’s initializers](https://medium.com/keepsafe-engineering/an-in-depth-look-at-kotlins-initializers-a0420fcbf546)**
>
> Yesterday I had a co-worker come to me with an interesting bug in a Kotlin class he was writing. He had a read-only property with an inline…
