🚀 Kotlin Companion Object – āĻŦ্āϰেāχāύ-āĻĢ্āϰেāύ্āĻĄāϞি āĻŦ্āϝাāĻ–্āϝা! đŸŽ¯

 

🚀 Kotlin Companion Object – āĻŦ্āϰেāχāύ-āĻĢ্āϰেāύ্āĻĄāϞি āĻŦ্āϝাāĻ–্āϝা! đŸŽ¯

🤔 Companion Object āĻ•ী?

👉 Kotlin-āĻ static keyword āύেāχ, āĻ•িāύ্āϤু āĻ…āύেāĻ• āϏāĻŽā§Ÿ Java-āĻāϰ static behavior āĻĒ্āϰ⧟োāϜāύ āĻšā§Ÿ!
👉 Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰে āĻāĻ•āϟি āĻ•্āϞাāϏেāϰ āĻ­েāϤāϰে āĻāĻŽāύ āĻ•িāĻ›ু āĻĄিāĻĢাāχāύ āĻ•āϰা āϝা⧟, āϝা āĻ•্āϞাāϏেāϰ āĻ…āĻŦāϜেāĻ•্āϟ āĻ›া⧜াāχ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰা āϏāĻŽ্āĻ­āĻŦ!
👉 āĻāϟি āĻŽূāϞāϤ Singleton-āĻāϰ āĻŽāϤো āĻ•াāϜ āĻ•āϰে!


đŸ”Ĩ Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰে Static-āĻāϰ āĻŦিāĻ•āϞ্āĻĒ!

Java-āĻāϰ static āĻĢিāϚাāϰেāϰ āĻŽāϤো āĻ•াāϜ āĻ•āϰে!
āĻ•োāύো āĻ…āĻŦāϜেāĻ•্āϟ āϤৈāϰি āύা āĻ•āϰেāĻ“ Companion Object-āĻāϰ āĻŽেāĻĨāĻĄ āĻāĻŦং āĻ­্āϝাāϞু āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰা āϝা⧟!

🚀 āωāĻĻাāĻšāϰāĻŖ:

class MathUtils {
    companion object {
        fun square(x: Int): Int {
            return x * x
        }
    }
}

fun main() {
    println(MathUtils.square(5)) // 25
}

🛠️ Output:

25

MathUtils.square(5) āĻ•āϞ āĻ•āϰāϤে āĻ•োāύো āĻ…āĻŦāϜেāĻ•্āϟ āϤৈāϰি āĻ•āϰāϤে āĻšā§Ÿāύি!
companion object āĻāϰ āĻ­েāϤāϰেāϰ āĻŽেāĻĨāĻĄ āĻ•্āϞাāϏেāϰ āύাāĻŽ āĻĻি⧟েāχ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰা āϏāĻŽ্āĻ­āĻŦ!


🎭 Companion Object-āĻāϰ āĻŽাāϧ্āϝāĻŽে Properties & Functions āϏংāϰāĻ•্āώāĻŖ āĻ•āϰা āϝা⧟!

class AppConfig {
    companion object {
        const val VERSION = "1.0.0"

        fun getAppInfo(): String {
            return "App Version: $VERSION"
        }
    }
}

fun main() {
    println(AppConfig.VERSION) // 1.0.0
    println(AppConfig.getAppInfo()) // App Version: 1.0.0
}

🛠️ Output:

1.0.0
App Version: 1.0.0

āĻāĻ–াāύে VERSION āĻāĻŦং getAppInfo() āĻĢাংāĻļāύ āĻ•্āϞাāϏেāϰ āύাāĻŽ āĻĻি⧟েāχ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰা āϝাāϚ্āĻ›ে!


đŸŽ¯ Companion Object āĻ•ে āύাāĻŽ āĻĻেāĻ“ā§Ÿা āϝা⧟!

👉 āĻ•িāĻ›ু āϏāĻŽā§Ÿ companion object āĻ•ে āύিāϰ্āĻĻিāώ্āϟ āύাāĻŽ āĻĻেāĻ“ā§Ÿা āĻĻāϰāĻ•াāϰ āĻšāϤে āĻĒাāϰে!

class UserManager {
    companion object Instance {
        fun getUserCount(): Int {
            return 10
        }
    }
}

fun main() {
    println(UserManager.getUserCount()) // 10
}

āĻāĻ–াāύে companion object Instance āύাāĻŽ āĻĻেāĻ“ā§Ÿা āĻšā§ŸেāĻ›ে, āĻ•িāύ্āϤু .getUserCount() āφāĻ—েāϰ āĻŽāϤোāχ āĻ•াāϜ āĻ•āϰāĻ›ে!


🚀 Companion Object + Factory Pattern! (Singleton Alternative)

👉 Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰে Factory Method āϤৈāϰি āĻ•āϰা āϝা⧟!
👉 āĻāϟি āĻ•্āϞাāϏেāϰ āĻ…āĻŦāϜেāĻ•্āϟ āϤৈāϰি āĻ•āϰাāĻ•ে āφāϰāĻ“ āύি⧟āύ্āϤ্āϰিāϤ āĻ•āϰে!

class User private constructor(val name: String) {
    companion object {
        fun create(name: String): User {
            return User(name)
        }
    }
}

fun main() {
    val user1 = User.create("Alice")
    val user2 = User.create("Bob")

    println(user1.name) // Alice
    println(user2.name) // Bob
}

āĻāĻ–াāύে User āĻ•্āϞাāϏেāϰ Constructor private, āĻ•িāύ্āϤু companion object āĻĻি⧟ে āύāϤুāύ āĻ…āĻŦāϜেāĻ•্āϟ āϤৈāϰি āĻ•āϰা āĻšā§ŸেāĻ›ে!
āĻāϟি Factory Pattern āĻ…āύুāϏāϰāĻŖ āĻ•āϰে!


📌 āϏংāĻ•্āώেāĻĒে:

✔️ Companion Object āĻšāϞো āĻāĻ•āϟি Singleton āĻ…āĻŦāϜেāĻ•্āϟ, āϝা āĻ•্āϞাāϏেāϰ āϏাāĻĨে āϝুāĻ•্āϤ āĻĨাāĻ•ে!
✔️ Java-āĻāϰ static āĻĢিāϚাāϰেāϰ āĻŦিāĻ•āϞ্āĻĒ āĻšিāϏেāĻŦে āĻ•াāϜ āĻ•āϰে!
✔️ āĻ•োāύো āĻ…āĻŦāϜেāĻ•্āϟ āϤৈāϰি āĻ›া⧜াāχ Companion Object-āĻāϰ āĻĒ্āϰāĻĒাāϰ্āϟি āĻ“ āĻŽেāĻĨāĻĄ āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰা āϝা⧟!
✔️ Factory Pattern, Static Constants, āĻāĻŦং Utility Functions āϤৈāϰি āĻ•āϰāϤে āĻĻাāϰুāĻŖ āĻ•াāϰ্āϝāĻ•āϰ!

đŸ”Ĩ āĻāĻ–āύ āĻĨেāĻ•ে Companion Object āĻŦ্āϝāĻŦāĻšাāϰ āĻ•āϰে āφāϰāĻ“ āĻ•্āϞিāύ āĻ“ āĻĒাāϰāĻĢāϰāĻŽ্āϝাāύ্āϏ āĻ­াāϞো āĻ•āϰা Kotlin āĻ•োāĻĄ āϞিāĻ–āϤে āĻĒাāϰāĻŦেāύ! 😎

Comments

Popular posts from this blog

AI (Artificial Intelligence) āϏāĻšāϜāĻ­াāĻŦে āĻŦোāĻাāϰ āϜāύ্āϝ āĻāĻ•āĻĻāĻŽ āĻŽāϏ্āϤিāώ্āĻ•-āĻŦাāύ্āϧāĻŦ āĻŦ্āϝাāĻ–্āϝা 🧠🤖

🧠 Regex āĻ•ী?

Kotlin-āĻ āĻ­েāϰিāϝ়েāĻŦāϞ āϘোāώāĻŖা āĻ•āϰাāϰ āĻĻুāχ āωāĻĒাāϝ়