Easy examples to showcase main and secondary / a number of constructors in Kotlin
This text was initially printed at vtsen.hashnode.dev on Jul 23, 2022.
In Kotlin, you might have one main and lots of secondary constructors.
Major Constructor
That is main constructor with one parameter.
class Instance constructor(personal val param1: String) {
init {
println("init is named.")
}
}
You may as well omit the constructor
key phrase.
class Instance(personal val param1: String) {
init {
println("init is named.")
}
}
You may NOT initialize in main constructor. As a substitute, it’s good to initialize your code in init{}
block.
Secondary Constructor
There are 2 secondary constructors under.
class Instance(personal val param1: String) {
init {
println("init is named.")
}
//First secondary constructor
constructor(
param1: String,
param2: String) : this(param1) {
println("Second constructor is named")
}
//Second secondary constructor
constructor(
param1: String,
param2: String,
param3: String) : this(param1) {
println("Third constructor is named")
}
}
Few essential notes right here:
- It’s essential to name the first constructor (e.g. calling
this(param1)
). Not likely! See subsequent part – Name One other Secondary Constructor - You may declare
var
orval
within the secondary constructor parameter - You may initialize your code in secondary constructor
Please word that main constructor along with the init{}
block is named 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 shall be like this.
init is named.
Third constructor is named
Name One other Secondary Constructor
As a substitute of calling the first constructor in your secondary constructor, you can too 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 is named.")
}
//First secondary constructor
constructor(
param1: String,
param2: String) : this(param1) {
println("Second constructor is named")
}
//Second secondary constructor
constructor(
param1: String,
param2: String,
param3: String) : this(param1, param2) {
println("Third constructor is named")
}
}
If I name the third constructor, the output appears to be like like this:
init is named.
Second constructor is named
Third constructor is named
Empty Major Constructor
That is an empty main constructor and secondary constructor instance.
class Instance() {
init {
println("init is named.")
}
constructor(param1: String): this() {
println("Second constructor is named")
}
}
Nonetheless, you do not actually need to name this()
in your secondary constructor. You additionally want to vary Instance()
to Instance
.
class Instance {
init {
println("init is named.")
}
constructor(param1: String) {
println("Second constructor is named")
}
}
Secondary Constructor Use case
I encountered the wants of utilizing secondary constructor once I need to inject the Hilt dependency into my View Mannequin.
%[https://vtsen.hashnode.dev/convert-view-model-to-use-hilt-dependency-injection]
I’ve code like this the place preview
parameter is used for @preview
jetpack compose. It’s set to true
solely in @preview
. Nonetheless, if I port this variation 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 variety of a number of constructors, have you ever? So, I documented this right here for my future reference.