Member-only story
Static vs. Companion Objects in Kotlin
Imagine you’re part of a team building a recipe book application. Each recipe is represented by a class. Now, you want to add some shared information or functionality that belongs to the recipe class as a whole, not just to individual recipes.
Static Members in Java
Think of static members in Java like a recipe book that has a special page where all chefs can write down their favorite ingredients. This special page is shared among all the recipe books, and whenever a chef adds an ingredient to it, it appears in all the recipe books.
In this analogy, the special page is like a static variable, and the act of adding ingredients is like calling a static method.
All the recipe books have access to the same special page, and all chefs can use it without needing to create their own recipe book.
Companion Objects in Kotlin
Now, consider companion objects in Kotlin as a group of chefs who collaborate to create new recipes. They have their own special kitchen where they share unique spices and tools.