This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll see how to add a constructor to our class and initialize our properties!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Right now, we've got our value and
our suit declared as immutable.
0:00
Which means they can never be changed
from zero and an empty string.
0:04
Let's see how to add a constructor and
use it to initialize our properties.
0:09
In Kotlin,
0:13
the constructor follows immediately
after the class name, check it out.
0:14
Up here after the word Card,
let's add some parentheses,
0:18
and then let's declare the parameters of
our constructor inside the parentheses.
0:23
First we have value, which is an Int.
0:28
And then we have suit, which is a String.
0:33
And finally we have faceUp,
which is a Boolean.
0:36
Once we've taken care of all
the parameters to our constructor,
0:41
we can add this optional init
block below our properties.
0:44
Whenever the constructor is called,
this init block will be called too.
0:50
Essentially, Kotlin just separates
the constructor into two pieces.
0:55
The parameters and
the initialization code.
1:00
An important thing to note about the
parameters of the constructor is that they
1:04
have a pretty limited scope.
1:08
We can only use these parameters
in initializing a property or
1:10
inside the init block.
1:15
Anywhere else is off limits.
1:17
So now, instead of initializing our
properties to potentially wrong values.
1:19
Let's initialize them to the values
passed into the constructor.
1:24
So value will equal value.
1:28
Suit will equal suit.
1:32
And faceUp can equal faceUp.
1:36
Now, we don't actually need
the init block for our card class.
1:39
But before we get rid of it,
let me show you something kind of cool.
1:43
We know that if we don't initialize our
value property, we'll get an error.
1:46
But we are allowed to initialize
it in the init block instead.
1:52
This.value = value.
1:56
All right, let's undo that and
as promised, let's delete the init block.
2:00
Then, to get this down to one line,
we just add value or
2:08
var in front of
the constructor parameters.
2:12
In Kotlin, adding val or
2:16
var in front of a constructor
parameter upgrades it into a property.
2:17
So we can massively simplify our
code by adding val in front of
2:23
this value parameter,
val in front of the suit parameter and
2:28
var in front of the faceUp parameter.
2:32
Then since we no longer need our
initial properties, let's delete them.
2:36
And since there's nothing
inside our class,
2:41
we can even delete the brackets too.
2:44
While we're at it,
let's also delete the JavaCard class.
2:49
Okay, just one more cool thing and then I
promise we'll move on from the Card class.
3:09
So each card in the deck starts face down.
3:14
And wouldn't it be nice if we could
have an alternate version of this
3:17
constructor that didn't require us
to pass in the faceUp parameter?
3:21
Well, with Kotlin,
we can specify default values for
3:25
our parameters just by
adding an equal sign.
3:28
So let's add =false.
3:31
And now, we can create new facedown card
instances with just a value and a suit.
3:34
Kotlin is an awesome language that gives
us a ton of new tools to play with.
3:40
But that also means things can
be a bit confusing at times.
3:45
If you're ever feeling stuck,
3:49
there's tons of excellent
documentation at kotlinlang.org.
3:50
Seriously, the Kotlin docs are way
better than the Android docs.
3:55
And don't forget about
your friends at Treehouse.
3:59
We're here to help too.
4:01
Coming up, we're going to expand from this
lonely card class into an entire deck.
4:03
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up