0
0
Kotlinprogramming~10 mins

Object declaration syntax in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object declaration syntax
Start
Declare object with 'object' keyword
Define properties and methods inside
Use object directly without instantiation
Access properties/methods
End
This flow shows how Kotlin's object declaration creates a singleton object with properties and methods that can be used directly.
Execution Sample
Kotlin
object Logger {
    val prefix = "Log:"
    fun printMessage(msg: String) {
        println("$prefix $msg")
    }
}

Logger.printMessage("Hello")
Defines a singleton object Logger with a property and a method, then calls the method to print a message.
Execution Table
StepActionEvaluationResult
1Declare object LoggerCreate singleton instanceLogger object created with prefix and printMessage
2Access Logger.prefixRetrieve value"Log:"
3Call Logger.printMessage("Hello")Execute functionPrints: Log: Hello
4EndNo more codeProgram ends
💡 Program ends after calling Logger.printMessage
Variable Tracker
VariableStartAfter 1After 2Final
Logger.prefixundefined"Log:""Log:""Log:"
Logger.printMessageundefinedfunction definedfunction definedfunction remains
Key Moments - 3 Insights
Why don't we create an instance of Logger with 'new'?
Because Logger is declared with 'object', Kotlin creates a singleton automatically, so no 'new' is needed. See execution_table step 1.
How can we access properties inside the object?
We use the object name directly, like Logger.prefix, as shown in execution_table step 2.
What happens when we call Logger.printMessage?
The function runs and prints the message with the prefix, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of Logger.prefix after step 2?
A"Log:"
B"Hello"
Cundefined
Dnull
💡 Hint
Check the 'Evaluation' and 'Result' columns in execution_table row 2.
At which step does the program print the message 'Log: Hello'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the 'Prints' action in execution_table.
If we tried to create Logger with 'new Logger()', what would happen?
AIt would create a new instance
BIt would cause a compile error
CIt would call printMessage automatically
DIt would print 'Log: Hello'
💡 Hint
Recall that 'object' declarations are singletons and cannot be instantiated with 'new'.
Concept Snapshot
Kotlin object declaration syntax:
- Use 'object Name' to create a singleton
- Define properties and methods inside
- Access directly by Name.property or Name.method()
- No need to instantiate with 'new'
- Useful for single-instance utilities or holders
Full Transcript
This example shows how Kotlin's object declaration creates a singleton named Logger. The object has a property 'prefix' and a method 'printMessage'. When the program runs, Kotlin creates the Logger object automatically (step 1). We access the prefix property directly (step 2) and call the printMessage method (step 3), which prints the message with the prefix. The program ends after this (step 4). This shows that objects declared with 'object' are singletons and do not require instantiation with 'new'.