In Kotlin, you create objects without using the new keyword. This makes the code simpler and easier to read.
Creating instances without new keyword in Kotlin
Introduction
Syntax
Kotlin
val instance = ClassName(arguments)You just call the class name like a function to create an instance.
No need for new keyword as in some other languages.
Examples
Person object with name "Alice" and age 30.Kotlin
val person = Person("Alice", 30)
Kotlin
val list = mutableListOf<Int>()
Rectangle object with width 5.0 and height 10.0.Kotlin
val rectangle = Rectangle(5.0, 10.0)
Sample Program
This program creates a Person instance without using new. It then prints the person's name and age.
Kotlin
class Person(val name: String, val age: Int) fun main() { val person = Person("Bob", 25) println("Name: ${person.name}, Age: ${person.age}") }
Important Notes
Kotlin simplifies object creation by removing the need for new.
Just call the class constructor like a function with parentheses and arguments.
This makes Kotlin code cleaner and easier to write.
Summary
Kotlin creates instances by calling the class name like a function.
No new keyword is needed.
This leads to simpler and more readable code.
Practice
1. In Kotlin, how do you create an instance of a class named
Car without using the new keyword?easy
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]
Hint: Remember: Kotlin uses class name with parentheses, no 'new' needed [OK]
Common Mistakes:
- Using 'new' keyword like in Java
- Trying to call a method like 'create Car()'
- Using 'Car.new()' which is invalid
2. Which of the following is the correct syntax to create an instance of class
Person in Kotlin?easy
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]
Hint: Use ClassName() directly to create instances in Kotlin [OK]
Common Mistakes:
- Adding 'new' keyword before class name
- Using 'Person.new()' which is invalid
- Trying to use 'create' keyword
3. What will be the output of the following Kotlin code?
class Dog(val name: String) {
fun bark() = "Woof! I am $name"
}
fun main() {
val dog = Dog("Buddy")
println(dog.bark())
}medium
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]
Hint: Instance properties are set by constructor arguments [OK]
Common Mistakes:
- Expecting 'dog' or 'name' literal instead of property value
- Thinking Kotlin requires 'new' keyword
- Assuming compilation error due to syntax
4. Identify the error in the following Kotlin code snippet:
class Book(val title: String)
fun main() {
val b = new Book("Kotlin Basics")
println(b.title)
}medium
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]
Hint: Remove 'new' keyword to fix instance creation error [OK]
Common Mistakes:
- Adding 'new' keyword from Java habits
- Thinking parentheses are optional
- Confusing data class requirement
5. Given the Kotlin class below, how can you create a list of
Choose the correct code snippet.
Person instances without using the new keyword?class Person(val name: String, val age: Int)
Choose the correct code snippet.
hard
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]
Hint: Use ClassName() inside listOf() to create instances [OK]
Common Mistakes:
- Adding 'new' keyword inside listOf()
- Using invalid 'Person.new()' syntax
- Trying to use 'create' keyword
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
