AMIT SHEKHAR’s Post

View profile for AMIT SHEKHAR

Founder @ Outcome School | Coder | Teacher | Mentor | Open Source | IIT 2010-14 | Android | Machine Learning | Backend

Internals of object class - Singleton in Kotlin Let me explain how Kotlin implements the object singleton pattern internally. For example, we created an object class as below: object OutcomeSchool { fun learnInternals() = "Hello dev, start learning the internals" } Internally, the compiler generates a final class with a private constructor: public final class OutcomeSchool {   private OutcomeSchool() { } } It creates a static INSTANCE field and initializes it in a static block: public final class OutcomeSchool { public static final OutcomeSchool INSTANCE;    static {      OutcomeSchool instance = new OutcomeSchool();      INSTANCE = instance;    } } Note: • Lazy initialization: The singleton instance is created only when it is accessed for the first time. • Thread safety: It is guaranteed by the JVM class loading mechanism. The static initialization is automatically thread-safe without additional synchronization. Follow Outcome School to learn internals. Keep Learning, Keep Sharing, and Keep Growing. Software engineers like you join Outcome School to achieve the outcome that is a high-paying tech job. Join Outcome School: https://outcomeschool.com #OutcomeSchool #SoftwareEngineer #Tech #AndroidDev #Android #Kotlin

  • No alternative text description for this image
Devidas Barge

Android Development Expert | Kotlin | Java | Banking & Finance Applications Specialist | Lead Engineer | Agile Methodologies | 10 Years of Experience

8mo

Impressive

Like
Reply
Ankush Yerawar

Senior Software Engineer

8mo

Hey AMIT SHEKHAR, Great content. But I think the byte code needs to be updated as a final variable cannot be assign a value at later point in time. It should look something like - public final class OutcomeSchool { public static final OutcomeSchool INSTANCE = new OutcomeSchool(); private OutcomeSchool() { } }

Like
Reply
Syed Mohsin Ali Shah

Android Team Lead @ AmexHub | Jetpack Compose | KMM | FinTech

8mo

Valuable info

Like
Reply
Mahabub Karim

Senior System Specialist (Android & iOS) @ United Finance | Developing cutting-edge Android & iOS apps | Machine Learning Enthusiast

8mo

This is great

Like
Reply
Pallavi .

Software Engineer @ Outcome School

8mo

Great

Like
Reply
Sarayu Prasad Yadav

Lead at Tata elxsi | Android developer | Android Automotive developer | Kotlin | flutter | jetpack compose

8mo

Great

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories