Avatar ·

Why does Kotlin need companion object and how does it replace static fields from Java?

Hi everyone! I recently started learning Kotlin after Java and encountered some confusion. In Java, I often used static fields and methods, for example, to create class constants or factory methods: ```java public class User { public static final int MIN_AGE = 18; public static User createGuest() { return new User("Guest"); } private String name; public User(String name) { this.name = name; } } ``` In Kotlin, I see that there is no static keyword, instead companion object is used. But I don't fully understand why an object is needed rather than just static. Why couldn't it be done like in Java? For example, I write like this: ```kotlin class User { companion object { val MIN_AGE = 18 fun createGuest() = User("Guest")