2.2 Class Constructors in Java
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
Hi and welcome back to the course.
I hope you guys are doing well. And in the last video lecture, we discussed classes in Java. And in this video lecture, we're going to be going over class constructors in Java.
Now, as always, I've listed out some learning objectives to make the goal of this video Lecter clear to you.
So the 1st 1 is that we are gonna be aiming to understand what constructors are
and essentially understand why we use constructors as well as why they are important.
And then after we have these two first learning objectives clear and down, we will go on to understanding how we can actually create constructors.
So what are class constructors to begin with?
So I've provided you with a definition which is taken directly from the Java Documentation website, and I'm gonna be putting in a link to this website in the glossary as well as in the supplementary materials so that you can go ahead and check it out for yourself.
So if we begin with the definition, it states that a class contains constructors that are invoked to create objects from the class blueprint.
Constructor declarations look like method declarations, except that they use the name of the class and have no return type.
All right, so for now, I'd like you to ignore the second part of the definition which says the method declaration stuff. And I'd like you to stick and concentrate more towards the first part of the definition.
So essentially all the constructor is is when you let's see you create a class and you want to make a version of that class, which or a specific instance of that class which we could call an object in Java or object oriented terms.
And don't worry if you know I don't know anything about objects yet. Although we've talked about it in our previous videos, we're going to be diving
more deeper into objects in our chapter that is related to that.
But here is an example of a constructor.
So let's say our class is called bicycle now. Some of the rules that a constructor needs to follow one of them being is that the name of the constructor has to be exactly the same as the class name.
So if my class is called bicycle, my constructor will also need to have the same name so bicycle.
And in this case, my constructor is taking in three parameters.
One is the start cadence. And then there's the start speed and finally, the start gear.
Now here I have some variables that would have been defined above this constructor, and I'm setting those variables to these parameters.
Now, once we have made this constructor, let's say we go into a different class and we want to make use of our bicycle class.
That is when the new Q word comes in.
So, in order to make use of the properties of the bicycle class, which in this case is the start speed the start gear and the start kittens, we will have to make a new object or a new instance of the class,
and that is done right here. First we say the name of the class and then the name off our bike. And in this case, I would like to say my bike. And then we make use of the nuclear word. And then we set,
Um, the value is inside the parentheses to these,
uh, parameters. So in this case, the start cadence would be 30. The start speed would be zero. And the start here would be eight.
And what it does is it basically creates space and memory, and it initialize is the fields of the bicycle constructor.
So if this doesn't make a lot of sense right now or if you have any confusion at all, do not worry, because we're gonna be going over creating constructors and making use of him in the coming video. And there everything will be, uh, clearly explained, as always in our labs, and then you'll have a better understanding.
So just to go over what we discussed in this video Elektra, we went over what constructors are
why we need to use constructors
as well as how we can create constructors.
All right, So, as I mentioned previously, the next video is gonna be about implementing constructors and actually using them.
So I look forward to seeing the next video. And if you have any questions at all, as always, feel free to contact me through Lincoln, and I'd be more than happy to answer any of your questions. Thank you very much for watching. And I'll see you in the next video lecture.