Creating instances without new keyword in Kotlin - Performance & Efficiency
We want to understand how the time it takes to create objects changes as we create more of them without using the new keyword.
How does the cost grow when making many instances this way?
Analyze the time complexity of the following code snippet.
class Person(val name: String)
fun createPeople(names: List): List {
return names.map { Person(it) }
}
This code creates a list of Person objects from a list of names without using the new keyword explicitly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a
Personinstance for each name in the list. - How many times: Once for each element in the input list
names.
Each name causes one new Person to be created, so the work grows directly with the number of names.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 creations |
| 100 | 100 creations |
| 1000 | 1000 creations |
Pattern observation: The number of operations grows in a straight line with input size.
Time Complexity: O(n)
This means the time to create all instances grows directly in proportion to how many you make.
[X] Wrong: "Creating instances without the new keyword is faster or slower in a way that changes time complexity."
[OK] Correct: The keyword new is just syntax; the actual work of creating each object still happens once per item, so time grows the same way.
Understanding how object creation scales helps you reason about performance in real apps, especially when working with collections and data transformations.
"What if we changed map to a recursive function creating instances? How would the time complexity change?"
Practice
Car without using the new keyword?Solution
Step 1: Understand Kotlin instance creation
Kotlin does not use thenewkeyword; instead, it calls the class name like a function.Step 2: Apply correct syntax for creating instance
To create an instance ofCar, simply writeCar()withoutnew.Final Answer:
val myCar = Car() -> Option AQuick Check:
Kotlin instance creation = Car() [OK]
- Using 'new' keyword like in Java
- Trying to call a method like 'create Car()'
- Using 'Car.new()' which is invalid
Person in Kotlin?Solution
Step 1: Recall Kotlin instance creation syntax
Kotlin creates instances by calling the class name with parentheses, no 'new' keyword.Step 2: Identify the correct syntax
Onlyval p = Person()matches Kotlin's syntax for creating an instance.Final Answer:
val p = Person() -> Option DQuick Check:
Instance creation = ClassName() [OK]
- Adding 'new' keyword before class name
- Using 'Person.new()' which is invalid
- Trying to use 'create' keyword
class Dog(val name: String) {
fun bark() = "Woof! I am $name"
}
fun main() {
val dog = Dog("Buddy")
println(dog.bark())
}Solution
Step 1: Understand instance creation and property assignment
The instancedogis created by callingDog("Buddy"), settingnameto "Buddy".Step 2: Evaluate the bark() function output
Thebark()function returns the string with$namereplaced by "Buddy".Final Answer:
Woof! I am Buddy -> Option CQuick Check:
dog.bark() = "Woof! I am Buddy" [OK]
- Expecting 'dog' or 'name' literal instead of property value
- Thinking Kotlin requires 'new' keyword
- Assuming compilation error due to syntax
class Book(val title: String)
fun main() {
val b = new Book("Kotlin Basics")
println(b.title)
}Solution
Step 1: Check instance creation syntax
The code usesnew Book("Kotlin Basics"), but Kotlin does not use thenewkeyword.Step 2: Identify correct Kotlin syntax
Instance creation should beBook("Kotlin Basics")withoutnew.Final Answer:
Using 'new' keyword which is invalid in Kotlin -> Option BQuick Check:
Kotlin instance creation = ClassName() without 'new' [OK]
- Adding 'new' keyword from Java habits
- Thinking parentheses are optional
- Confusing data class requirement
Person instances without using the new keyword?class Person(val name: String, val age: Int)
Choose the correct code snippet.
Solution
Step 1: Understand Kotlin instance creation in collections
Instances are created by calling the class name with parentheses, no 'new' keyword.Step 2: Check list creation syntax
listOf()accepts instances created byPerson("Alice", 30)andPerson("Bob", 25).Final Answer:
val people = listOf(Person("Alice", 30), Person("Bob", 25)) -> Option AQuick Check:
Instances in list = Person() calls without 'new' [OK]
- Adding 'new' keyword inside listOf()
- Using invalid 'Person.new()' syntax
- Trying to use 'create' keyword
Ready to prove it?
Pass the test and this pattern moves unknown
