2.2 Class Constructors in Java
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
Hi and welcome back to the course.
00:02
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.
00:15
Now, as always, I've listed out some learning objectives to make the goal of this video Lecter clear to you.
00:24
So the 1st 1 is that we are gonna be aiming to understand what constructors are
00:30
and essentially understand why we use constructors as well as why they are important.
00:38
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.
00:52
So what are class constructors to begin with?
00:57
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.
01:14
So if we begin with the definition, it states that a class contains constructors that are invoked to create objects from the class blueprint.
01:26
Constructor declarations look like method declarations, except that they use the name of the class and have no return type.
01:34
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.
01:48
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.
02:07
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
02:15
more deeper into objects in our chapter that is related to that.
02:19
But here is an example of a constructor.
02:23
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.
02:38
So if my class is called bicycle, my constructor will also need to have the same name so bicycle.
02:46
And in this case, my constructor is taking in three parameters.
02:50
One is the start cadence. And then there's the start speed and finally, the start gear.
02:57
Now here I have some variables that would have been defined above this constructor, and I'm setting those variables to these parameters.
03:06
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.
03:15
That is when the new Q word comes in.
03:20
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,
03:35
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,
03:51
Um, the value is inside the parentheses to these,
03:54
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.
04:03
And what it does is it basically creates space and memory, and it initialize is the fields of the bicycle constructor.
04:15
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.
04:34
So just to go over what we discussed in this video Elektra, we went over what constructors are
04:42
why we need to use constructors
04:45
as well as how we can create constructors.
04:50
All right, So, as I mentioned previously, the next video is gonna be about implementing constructors and actually using them.
04:58
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.
Up Next
Similar Content