Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's different between two constructors?

Tags:

kotlin

What different between this type of constructor?

class ColorsArray(context: Context) {}

and

class ColorsArray(var context: Context){}
like image 893
manwhotrycoding Avatar asked Dec 06 '25 09:12

manwhotrycoding


1 Answers

The second class not only declares a constructor that takes a Context, but it also has a property named context where it saves the value passed into the constructor. You can then access this like so:

val colorsArray = ColorsArray(context)
println(colorsArray.context)

Since you've declared it as a var and not a val, this can also be reassigned.

colorsArray.context = someOtherContext

Properties declared in the primary constructor are covered in the docs here.

like image 94
zsmb13 Avatar answered Dec 09 '25 13:12

zsmb13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!