How to Pass Object As Argument In Kotlin?

9 minutes read

In Kotlin, you can pass an object as an argument to a function by simply specifying the object's type in the function parameter. You can then access the properties and methods of the object within the function.


For example, if you have a class named Person with properties name and age, you can pass an instance of this class as an argument to a function like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Person(val name: String, val age: Int)

fun printPersonDetails(person: Person) {
    println("Name: ${person.name}, Age: ${person.age}")
}

fun main() {
    val person = Person("Alice", 30)
    printPersonDetails(person)
}


In this example, the printPersonDetails function takes a Person object as an argument and prints out the person's name and age. The main function creates an instance of Person and calls printPersonDetails with that instance as an argument.

Best Kotlin Books to Read in 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Rating is 4.9 out of 5

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

3
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Rating is 4.8 out of 5

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

4
Kotlin in Action

Rating is 4.7 out of 5

Kotlin in Action

5
Kotlin Design Patterns and Best Practices: Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin, 2nd Edition

Rating is 4.6 out of 5

Kotlin Design Patterns and Best Practices: Build scalable applications using traditional, reactive, and concurrent design patterns in Kotlin, 2nd Edition

6
Head First Kotlin: A Brain-Friendly Guide

Rating is 4.5 out of 5

Head First Kotlin: A Brain-Friendly Guide

7
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.4 out of 5

Kotlin Cookbook: A Problem-Focused Approach

8
How to Build Android Apps with Kotlin: A practical guide to developing, testing, and publishing your first Android apps, 2nd Edition

Rating is 4.3 out of 5

How to Build Android Apps with Kotlin: A practical guide to developing, testing, and publishing your first Android apps, 2nd Edition

9
Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

Rating is 4.2 out of 5

Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

10
Java to Kotlin: A Refactoring Guidebook

Rating is 4.1 out of 5

Java to Kotlin: A Refactoring Guidebook


How to pass custom objects as arguments in Kotlin?

To pass custom objects as arguments in Kotlin, you can simply declare the object as a parameter in the function signature. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
data class Person(val name: String, val age: Int)

fun main() {
    val person = Person("Alice", 25)
    
    greetPerson(person)
}

fun greetPerson(person: Person) {
    println("Hello, ${person.name}! You are ${person.age} years old.")
}


In the example above, we have a custom Person class with name and age properties. We then create an instance of Person and pass it as an argument to the greetPerson function. The greetPerson function takes a Person object as an argument and prints a personalized greeting using the object's properties.


You can pass custom objects in the same way to any function or method in your Kotlin code.


How to handle modifications to objects passed as arguments in Kotlin?

In Kotlin, objects passed as arguments are passed by reference, so any modifications made to the object within the function will affect the original object that was passed in.


If you want to avoid modifying the original object, you can make a copy of the object inside the function before making any changes. You can create a copy of the object using the copy() function or by manually creating a new object and copying the values of the original object into it.


Another approach is to use immutable objects or data classes, which do not allow modifications after they are created. This way, you can ensure that the object passed as an argument will not be modified accidentally.


It is also a good practice to clearly document in the function documentation if the object passed as an argument will be modified. This will help the caller of the function to understand the side effects of the function and handle the modifications accordingly.


How to pass objects with default values as arguments in Kotlin?

In Kotlin, you can pass objects with default values as arguments by simply not providing a value for that argument when calling the function. The function will then use the default value specified in the function's declaration.


Here is an example of a function that takes an object with a default value as an argument:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data class Person(val name: String, val age: Int = 0)

fun printPersonInfo(person: Person) {
    println("Name: ${person.name}, Age: ${person.age}")
}

fun main() {
    val person1 = Person("Alice")
    val person2 = Person("Bob", 25)
    
    printPersonInfo(person1) // Will print: Name: Alice, Age: 0
    printPersonInfo(person2) // Will print: Name: Bob, Age: 25
}


In this example, the Person class has a default value of 0 for the age property. When calling the printPersonInfo function with only the name property provided, it will use the default value for age.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To invoke Dart code from Kotlin code, you can use the platform channel provided by Flutter. This allows communication between the Dart and Kotlin code in your app.First, define a method in your Dart code that you want to invoke from Kotlin. Then, create a Meth...
To import Kotlin functions into Java classes, first you need to create a Kotlin file with the functions you want to use. Make sure to mark these functions as @JvmStatic so they can be accessed statically in Java. Next, compile your Kotlin file into a .jar file...
In Kotlin, you can annotate type arguments by using the @ symbol followed by the annotation name before the type argument. This can be done when declaring a generic class or function. Annotations can be used to provide additional information or constraints on ...