SoFunction
Updated on 2025-04-13

Function of constructors in kotlin

1. The role of constructor

The function of constructor(name: String, age: Int, email: String, email_test: String) : this(0, name, age, email, email_test)

two

In Kotlin, the data class is a special class that is mainly used to store data. Each data class has a main constructor that initializes its properties. If other ways to initialize these properties can be achieved by defining secondary constructors.

In this code:

constructor(name: String, age: Int, email: String, email_test: String) : this(0, name, age, email, email_test)

This line of code defines a helper constructor and delegates it to the main constructor. The specific functions are as follows:

Define auxiliary constructor

constructor(name: String, age: Int, email: String, email_test: String)A new constructor is defined, accepting four parameters:name, age, email, andemail_test

Delegate to the main constructor

  • : this(0, name, age, email, email_test)Indicates that this auxiliary constructor will call the main constructor to perform the actual initialization work.
  • Here,thisA keyword indicates that another constructor (i.e., the main constructor) that calls the same class.
  • parameter(0, name, age, email, email_test)is the specific value passed to the main constructor. Here0It is for initializationidFields, becauseidFields are used@PrimaryKey(autoGenerate = true)Annotations are usually automatically generated when inserting into the database, but an initial value is still required when constructing the object.

Specifically, the function of this code is:

  • Provides a convenient way to createUserObjects without explicitly settingidField.
  • Allow users to passname, age, email, andemail_testParameters to createUserThe object will automaticallyidInitialize to0

Here is a complete code example to better understand its context:

package .roomencapsulation3
import 
import 
import 
import 
@Entity(tableName = "users")
data class User(
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0,
    @ColumnInfo(name = "user_name")
    val name: String,
    @ColumnInfo(name = "user_age")
    val age: Int,
    @ColumnInfo(name = "email")    // Add new fields    val email: String = "",
    @ColumnInfo(name = "email_test")    // Add new fields    val email_test: String = ""
) {
    constructor(name: String, age: Int, email: String, email_test: String) : this(0, name, age, email, email_test)
    @Ignore
    val temporaryToken: String? = null
}

In this example:

  • Main constructorUser(id: Int, name: String, age: Int, email: String, email_test: String)Used to initialize all fields.
  • Auxiliary constructorUser(name: String, age: Int, email: String, email_test: String)Provides an easy way to allow unexplicit settingsidfield, instead let the Room generate it automatically.

The advantage of this design is that it simplifies the creation process of the object while ensuring that all necessary fields are correctly initialized.

3. The order of call of primary constructor and secondary constructor

Call order

Calling the helper constructor

  • When you callUser(name = "Alice", age = 30, email = "alice@", email_test = "test@")When , the helper constructor is actually calling.

Delegate to the main constructor

  • The auxiliary constructor is internally passedthis(0, name, age, email, email_test)Delegate to the main constructor.
  • Main constructorUser(id: Int, name: String, age: Int, email: String, email_test: String)These parameters are received and initialized.

Complete sample code

The following is completeUserClass definitions and their usage examples help you better understand the call order and initialization process.

Detailed explanation of the call order

Calling the helper constructor

val userA = User(name = "Alice", age = 30, email = "alice@", email_test = "test@")

This line of code is calledUserHelper constructors in the class:

constructor(name: String, age: Int, email: String, email_test: String) : this(0, name, age, email, email_test)

Delegate to the main constructor

  • The auxiliary constructor is internally passedthis(0, name, age, email, email_test)Delegate to the main constructor.
  • The main constructor receives these parameters and initializes them:
data class User(
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0,
    @ColumnInfo(name = "user_name")
    val name: String,
    @ColumnInfo(name = "user_age")
    val age: Int,
    @ColumnInfo(name = "email")    // Add new fields    val email: String = "",
    @ColumnInfo(name = "email_test")    // Add new fields    val email_test: String = ""
)

Summarize

Helper constructor: provides an easy way to initialize objects, especially when certain fields do not need to be explicitly set (such as automatically generatedid). Call order:
1. First call the helper constructor.
2. The auxiliary constructor passesthis(...)Delegate to the main constructor.
3. The main constructor completes the actual initialization work.

In this way, you can flexibly initializeUserObject and make sure all necessary fields are set correctly.

This is the end of this article about the role of constructors in kotlin. For more related content of kotlin constructors, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!