How to properly use secondary constructors in Kotlin? Examples and syntax
Hi everyone! I just started learning Kotlin after Java and encountered a problem when creating classes with multiple constructors.
In Java I used to do this:
```java
public class User {
private String name;
private int age;
public User(String name) {
this.name = name;
this.age = 0;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
```
But in Kotlin I see some strange syntax with the `constructor` keyword and a colon. I tried to write something similar:
```kotlin
class User {
var name: String = ""
var age: Int = 0
constructor(name: String) {
this.name = name
}
constructor(name: String, age: Int) {
this.name =
```
No answers yet.
Log in to leave an answer
Blogs