Difference between primary and secondary

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

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

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

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.

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.

1 Like