

Developers consider this to be more optimal than Java’s static member usage. The use is almost identical to static in Java but it is a real object rather than a static class member. When the main class is called for the first time, an instance of this class is created that contains the internal properties and methods that the main class instance can call.

Kotlin proposes that companion objectnest static classes are in the main classes.

Why is it so when we can only need objects as instances? In Java, we have objects that are instance members and classes with static components. Classes are not objects but only instances of the class are objects. In OOP, something that is not an object is a big limp. Kotlin is an object-oriented programming language (OOP). Static does not comply with the OOP style

Therefore, we do not need to declare static components inside classes anymore, so we do not need static keywords. Components outside the class will default to static as top-class levels, on par with classes. Because of the need to place static components inside classes, we need to add static keywords to distinguish components at the class level from components at the instance level.īut not so Kotlin, it allows us to declare both the outside of the class as constants, variables, and functions outside of the class. This creates a problem when we want to declare static components, not depending on the instance of the class, and can be shared in many places. In Java, everything must be declared inside a class. So, for what reason do Kotlin developers want to remove this powerful keyword? Kotlin is not as restrictive as Java Kotlin is fully backward compatible with Java, but not everything is preserved in Java.
