There are times where you want to make sure that only a single instance of a given type can be created, such as if you want to use a single object to coordinate actions across an entire application. In these situations, you can use the object keyword to make an object declaration.
An object declaration defines a class declaration and creates an instance of it in a single statement.
And when you include it in the top level of a source file or package, only one instance of that type will ever be created.
Companion Objects
A companion object is a special type of object declaration defined inside a class. It is used to hold members that belong to the class itself rather than a specific instance, similar to static members in Java.
Any properties or functions declared inside a companion object are shared across all class instances.
Telegram Group: https://t.me/androidNinjaWarrior
Guide to watch:
00:17 What is the requirement of Object declaration in Kotlin?
01:11 How to declare an Object in Kotlin?
04:30 How to create companion object in Kotlin?
10:05 How to use an object as expressions
11:24 How to create data object in Kotlin?
#object #companionObject #objectDeclaration #expressions #dataObject #singleton #kotlin #advanceKotlin
Disclaimer- Some content are used for educational purposes under fair use.
Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for "fair use" for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use. All credit for copyright material used in the video goes to the respected Owner.
Thank you guys.