‫Instructor: Hi, We're going to deep dive
‫into the classes in this lecture
‫and we're gonna learn a very important concept
‫called constructor, okay?
‫So far we have created our own object
‫but there are some better ways to deal with this kind of
‫situations in Java at our under development.
‫So in order to learn about constructor,
‫I'm just gonna create it.
‫So if you come over here and right click and say generate
‫or if you come over here to coding menu and say generate
‫it will give you a menu.
‫And as you can see
‫we have the constructor in the first place.
‫Now we are looking for constructor
‫because that's what we are going to learn
‫and deep dive in this lecture.
‫As you can see, once we come over here
‫it will ask what kind of properties that we want to include.
‫Hit shift and select everything and hit enter or okay.
‫And as you can see, it created our first constructor.
‫So what is a constructor anyway?
‫Yeah, it constructs some object, but what does it really do?
‫So if you create that, that constructor,
‫and if you come over here,
‫now you will see you're getting some kind of an error.
‫So what is the error over here?
‫So we are creating an instance,
‫an object out of this Musicians class.
‫Right?
‫So when we create a new instance or a new object
‫constructor is the first method to be executed.
‫This is the first thing happens
‫right after you create your own object.
‫And in this case, as you can see,
‫we are doing some kind of initialization like
‫this.name = name.
‫So let me print out something over here,
‫so we can understand it better, right?
‫So as you can see, we have all this codes here.
‫this.instrument = instrument
‫this.age = age.
‫And it doesn't make sense when you first look into it.
‫However, it actually makes sense.
‫Since we have these properties inside of our class.
‫Now we can ask for an input
‫as a parameter inside of our public Musicians method.
‫And then we can make this.name
‫equals to that parameter.
‫You don't know what this is.
‫I'm going to explain it, don't worry.
‫Right now focus on this.
‫We're going to take some parameters
‫and we're gonna make it equal to the properties
‫that we have defined inside of our class.
‫Now you can change it if you want, like instrument2, age2,
‫and you can actually differentiate these names
‫from these parameters.
‫So name over here, this.name refers
‫to the attributes of this class.
‫So this is, these are called attributes
‫and these are called parameters.
‫So we are asking for parameters
‫inside of any other class inside a main activity
‫and developer gives some instrument2, name2, age2
‫And by saying this.name, we refer to the name,
‫instrument, and age of the class.
‫So this refers to the class,
‫this refers musicians.
‫You can name them, name, instrument, or age
‫and you can name them whatever you want.
‫But this is the main convention.
‫They have the same names.
‫So you can do it like this.
‫Let me call this constructor called
‫and let me come over here
‫and see what kind of error we have and as you can see
‫Musicians now asking for parameter
‫and we don't give,
‫if we don't give any parameters, it won't even work.
‫Now we have to give age, instrument and name
‫rather than giving those values afterwards.
‫When we say new Musicians, it'll ask for a name
‫and as you can see, it actually gives us some hints as well.
‫Once we hit double quotation marks,
‫now I gotta say James, Guitar and 50,
‫and it'll just make it equal to James, Guitar and 50.
‫Now if I say james.age one more time
‫I can reach that value as well, right?
‫So let me try to print this out.
‫I will say james.instrument, for example.
‫It'll print out guitar.
‫Of course, in order to make this work,
‫I have to call this makeMusicians Method
‫inside of any other method like onCreate
‫or any other method that will be executed
‫without my intervention.
‫So let me call this inside of onCreate methods,
‫like here, okay?
‫And this will be called,
‫and we are gonna create an instance,
‫and then we are going to print out the instrument
‫of that instance.
‫So let's follow this.
‫Here we go.
‫Now constructor called because we created James,
‫and then after that
‫the instrument of James got printed in the logs.
‫So this is how you work with constructors.
‫This is how you work with classes generally.
‫So let's stop here and continue within the next one.