HomeAndroidPerceive Kotlin A number of Constructors

Perceive Kotlin A number of Constructors


In Kotlin, you’ve gotten one main and plenty of secondary constructors.

Main Constructor

That is main constructor with one parameter.

class Instance constructor(personal val param1: String) {
    init {
        println("init known as.")
    }
}

You too can omit the constructor key phrase.

class Instance(personal val param1: String) {
    init {
        println("init known as.")
    }
}

You possibly can NOT initialize in main constructor. As an alternative, you could initialize your code in init{} block.

Secondary Constructor

There are 2 secondary constructors under.

class Instance(personal val param1: String) {

    init {
        println("init known as.")
    }

    
    constructor(
        param1: String, 
        param2: String) : this(param1) {

        println("Second constructor known as")
    }

    
    constructor(
        param1: String, 
        param2: String, 
        param3: String) : this(param1) {

        println("Third constructor known as")
    }
}

Few vital notes right here:

  • You should name the first constructor (e.g. calling this(param1)). Not likely! See subsequent part – Name One other Secondary Constructor
  • You possibly can declare var or val within the secondary constructor parameter
  • You possibly can initialize your code in secondary constructor

Please notice that main constructor along with the init{} block known as first earlier than the secondary constructor initialization.

So, if I name the third constructor,

val obj = Instance(param1="1", param2="2", param3="3")

the output will probably be like this.

init known as.
Third constructor known as

Name One other Secondary Constructor

As an alternative of calling the first constructor in your secondary constructor, you can even name one other secondary constructor.

On this instance, the second secondary constructor calls the primary secondary constructor.

class Instance(personal val param1: String) {

    init {
        println("init known as.")
    }

    
    constructor(
        param1: String,
        param2: String) : this(param1) {

        println("Second constructor known as")
    }

    
    constructor(
        param1: String,
        param2: String,
        param3: String) : this(param1, param2) {

        println("Third constructor known as")
    }
}

If I name the third constructor, the output appears to be like like this:

init known as.
Second constructor known as
Third constructor known as

Empty Main Constructor

That is an empty main constructor and secondary constructor instance.

class Instance() {
    init {
        println("init known as.")
    }

    constructor(param1: String): this() {
        println("Second constructor known as")
    }
}

Nevertheless, you do not really want to name this() in your secondary constructor. You additionally want to alter Instance() to Instance.

class Instance {
    init {
        println("init known as.")
    }

    constructor(param1: String) {
        println("Second constructor known as")
    }
}

Secondary Constructor Use case

I encountered the wants of utilizing secondary constructor after I wish to inject the Hilt dependency into my View Mannequin.

I’ve code like this the place preview parameter is used for @preview jetpack compose. It’s set to true solely in @preview. Nevertheless, if I port this modification to make use of Hilt dependency injection, it fails to inject this dependency.

class MainViewModel(
    personal val repository: ArticlesRepository,
    preview: Boolean = false,
) : ViewModel() {
   
}

Thus, I break this to secondary constructor under.

class MainViewModel(
    personal val repository: ArticlesRepository) : ViewModel() {
    constructor (
        repository: ArticlesRepository, 
        preview: Boolean) : this(repository) {
        
    }
}

So I take advantage of @Inject constructor for the first constructor and the secondary constructor is used for @preview.

With Hilt implementation, it appears to be like like this

@HiltViewModel
class MainViewModel
    @Inject constructor(
        personal val repository: ArticlesRepository,
    ) : ViewModel() {

    constructor (
        repository: ArticlesRepository, 
        preview: Boolean) : this(repository) {
        
    }
    
}

Conclusion

I have never used a whole lot of a number of constructors, have you ever? So, I documented this right here for my future reference.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments