Search this site
Embedded Files
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
  • More
    • Home
    • Institute Coaching Programs
    • Android curriculum
      • Android Topics
        • Notification
        • Broadcast & Broadcast Receiver
        • Content Providers
        • Activities
          • Activity and Lifecycle
          • Saving state
          • Tasks and the back stack
        • Services
          • Services
          • Foreground Service
          • Bound Service
        • Dependency Injection
        • Workmanager
        • Fragment Findings*
        • Navigation findings*
        • Permissions Findings*
        • Room & Datastore findings*
      • Kotlin Topics
        • Nullables
        • Exceptions
        • Conditions and Loops
        • Arrays
        • Collections
        • Collection Functions
        • Class & Object
        • OOP Concepts
        • Scope Function
        • Visibility Modifiers
        • Generics
        • Kotlin Classes Types
          • Enums
          • Sealed Class
          • Data Classes
          • Nested and inner classes
          • Object expressions and declarations
        • Kotlin Functions Types
          • Functions
          • Extension functions
          • Higher-order functions and lambdas
        • Couroutines
        • Kotlin Flows
      • Interview Questions
        • Android Interview Questions
    • Products
      • Shabri
      • WedExhibit
    • About

Object expressions and declarations↗

Object Declaration↗
Data Object↗
Object expressions↗
Creating anonymous objects from scratch
Inheriting anonymous objects from supertypes
Accessing variables from anonymous objects
Companion objects↗
Semantic difference between object expressions and declarations↗
Video Notes

Object Declaration↗

The Singleton pattern can be useful in several cases, and Kotlin makes it easy to declare singletons:

The initialization of an object declaration is thread-safe and done on first access.


object DataProviderManager {

    fun registerDataProvider() {}

}


This is called an object declaration, and it always has a name following the object keyword. Just like a variable declaration, an object declaration is not an expression, and it cannot be used on the right-hand side of an assignment statement.


To refer to the object, use its name directly:

DataProviderManager.registerDataProvider(...)

Data Object↗

Just like data classes, you can mark an object declaration with the data modifier. This instructs the compiler to generate a number of functions for your object:

  • toString() returns the name of the data object

  • equals()/hashCode() pair

  • No copy() & componentN()function. Because a data object declaration is intended to be used as singleton objects, no copy() function is generated.

Object expressions↗

Object expressions create objects of anonymous classes, that is, classes that aren't explicitly declared with the class declaration. Such classes are useful for one-time use. You can define them from scratch, inherit from existing classes, or implement interfaces. Instances of anonymous classes are also called anonymous objects because they are defined by an expression, not a name.

Creating anonymous objects from scratch

Object expressions start with the object keyword.

if you just need an object that doesn't have any nontrivial supertypes, write its members in curly braces after object:

Inheriting anonymous objects from supertypes

To create an object of an anonymous class that inherits from some type (or types), specify this type after object and a colon (:). Then implement or override the members of this class as if you were inheriting from it:

Accessing variables from anonymous objects

The code in object expressions can access variables from the enclosing scope:

Companion objects↗

An object declaration inside a class can be marked with the companion keyword:


class MyClass {

    companion object Factory {

        fun create(): MyClass = MyClass()

    }

} 


Members of the companion object can be called simply by using the class name as the qualifier:


val instance = MyClass.create()


Create() method is not static if you check in java. To make it static we have to explicitly write @JvmStatic


Semantic difference between object expressions and declarations↗


There is one important semantic difference between object expressions and object declarations:

  • Object expressions are executed (and initialized) immediately, where they are used.

  • Object declarations are initialized lazily, when accessed for the first time.

  • A companion object is initialized when the corresponding class is loaded (resolved) that matches the semantics of a Java static initializer.



Video Notes

Object Declaration is a single pattern

  • It is declared like a class but without class name. the name is an object name and by using that name directly we can access the property of object

  • only one object is created with same name

  • there is no constructor in object as there is a single object

  • like class we can inherit the class to object

Object Expression is an annonyms object

  • follow the same philosophy as object

You can create an object inside the class

  • Classname.Object name.method() - you can access like this

  • If you create multipe instance of class then also the inner object's object are creating once

  • Class can use the properties of companion object as its a companion of the class so with companion: 

Classname.method() (no object name required)

if you check this method() in java code then it is not static method

  • Class has only one companion object

Object which is a singleton in a behaviour is created for social media like button.

Object expression, known as a annonyms class is used like this

we can inherit another class or interface into object anonymous class

Factory pattern. 

use of private constructor & companion object

A brand by Jagrut Soni
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse