# Learning Kotlin Constructor as a Java Developer

DevFeed: [Learning Kotlin Constructor as a Java Developer](<https://devfeed.tech/articles/learning-kotlin-constructor-as-a-java-developer-19837.md>)

Original publisher: [Read original article](<https://tech.gc.com/learning-kotlin-constructor-as-a-java-developer/>)

Author: GameChanger

Published: 2018-03-27T20:18:49Z

Content type: article

Language: en

Sources: [GameChanger](<https://devfeed.tech/sources/gamechanger.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Java](<https://devfeed.tech/topics/java.md>), [Learning](<https://devfeed.tech/topics/learning.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [developer](<https://devfeed.tech/tags/developer.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [learning](<https://devfeed.tech/tags/learning.md>)

## AI overview

A tutorial for Java developers moving to Kotlin that explains primary and secondary constructors, constructor invocation order, initialization timing, and Kotlin's default-argument approach to overloading. It also demonstrates an explicitly discouraged workaround for imitating Java-style constructor behavior.

## Source excerpt

I have been developing Android apps in Java for years. I recently joined GameChanger and was excited to learn that GameChanger is using Kotlin. I originally thought that moving to Kotlin would be as simple as learning some new syntax, but I discovered that there was more to it. After a brief learning period, I'm up and running working in Kotlin. In this article, I'll save you some of the trial-and-error by introducing some important concepts about constructors for those making the jump from Java to Kotlin. Not All Constructors are Created Equal in Kotlin In Java, all constructors are equal in a sense. public class Person { String name; int age; public Person(Person p) { name = p.name; age = p.age; } public Person(String n, int a) { name = n; age = a; } } Take the above example, Person(Person p) and Person(String name, int age) can be used independently. Of course, you can choose to have one calling another, but it's not required by the language. In Kotlin there is always a primary constructor. Any additional constructors are secondary constructors. The primary constructor is always incorporated into the class header. class Person(n: String, a: Int) { var name: String = n var age: Int = a } The variables name and age are initialized with n and a. In fact, n and a are available anywhere in the class for variable initialization. class Person(n: String, a: Int) { var name: String = n var age: Int = a var nameX2: String = n + n init { println("The age is: " + a) } } Any other constructors would become secondary constructors which are required to call the primary constructor in the very beginning. class Person(n: String, a: Int) { var name: String = n var age: Int = a constructor(p: Person) : this(p.name, p.age) { } } The Order of Creation So, what is the order of processing when calling a secondary constructor? The primary constructor is invoked first, which triggers all the initialization from top to bottom. Then, the body of the secondary constructor is executed. class