Constructors

Complete Step By Step Java for Testers Classes And Methods -> Object Oriented Concepts
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.80
List Price:  €131.33
You save:  €37.52
£80.29
List Price:  £112.41
You save:  £32.12
CA$137.56
List Price:  CA$192.59
You save:  CA$55.03
A$155.72
List Price:  A$218.02
You save:  A$62.29
S$136.13
List Price:  S$190.59
You save:  S$54.46
HK$783.15
List Price:  HK$1,096.44
You save:  HK$313.29
CHF 90.82
List Price:  CHF 127.16
You save:  CHF 36.33
NOK kr1,103.56
List Price:  NOK kr1,545.03
You save:  NOK kr441.46
DKK kr700
List Price:  DKK kr980.03
You save:  DKK kr280.02
NZ$169.65
List Price:  NZ$237.52
You save:  NZ$67.86
د.إ367.23
List Price:  د.إ514.13
You save:  د.إ146.90
৳10,973.26
List Price:  ৳15,363.01
You save:  ৳4,389.74
₹8,346.81
List Price:  ₹11,685.87
You save:  ₹3,339.05
RM478.30
List Price:  RM669.64
You save:  RM191.34
₦129,687.03
List Price:  ₦181,567.03
You save:  ₦51,880
₨27,846.09
List Price:  ₨38,985.64
You save:  ₨11,139.55
฿3,684.97
List Price:  ฿5,159.11
You save:  ฿1,474.13
₺3,259.29
List Price:  ₺4,563.14
You save:  ₺1,303.85
B$524.14
List Price:  B$733.82
You save:  B$209.68
R1,920
List Price:  R2,688.08
You save:  R768.07
Лв183.64
List Price:  Лв257.10
You save:  Лв73.46
₩138,024.19
List Price:  ₩193,239.39
You save:  ₩55,215.20
₪378.55
List Price:  ₪529.99
You save:  ₪151.43
₱5,756.02
List Price:  ₱8,058.66
You save:  ₱2,302.64
¥15,447.20
List Price:  ¥21,626.70
You save:  ¥6,179.50
MX$1,726.94
List Price:  MX$2,417.79
You save:  MX$690.84
QR366.07
List Price:  QR512.51
You save:  QR146.44
P1,385.95
List Price:  P1,940.39
You save:  P554.43
KSh13,348.66
List Price:  KSh18,688.66
You save:  KSh5,340
E£4,832.56
List Price:  E£6,765.78
You save:  E£1,933.22
ብር5,710.80
List Price:  ብር7,995.35
You save:  ብር2,284.55
Kz83,585.80
List Price:  Kz117,023.47
You save:  Kz33,437.66
CLP$96,622.33
List Price:  CLP$135,275.13
You save:  CLP$38,652.80
CN¥724.04
List Price:  CN¥1,013.69
You save:  CN¥289.64
RD$5,929.75
List Price:  RD$8,301.89
You save:  RD$2,372.13
DA13,457.15
List Price:  DA18,840.55
You save:  DA5,383.40
FJ$227.57
List Price:  FJ$318.61
You save:  FJ$91.03
Q780.47
List Price:  Q1,092.69
You save:  Q312.22
GY$20,930.40
List Price:  GY$29,303.40
You save:  GY$8,372.99
ISK kr14,100.58
List Price:  ISK kr19,741.38
You save:  ISK kr5,640.80
DH1,016.63
List Price:  DH1,423.32
You save:  DH406.69
L1,790.82
List Price:  L2,507.22
You save:  L716.40
ден5,785.11
List Price:  ден8,099.38
You save:  ден2,314.27
MOP$806.50
List Price:  MOP$1,129.14
You save:  MOP$322.63
N$1,910.91
List Price:  N$2,675.35
You save:  N$764.44
C$3,692.82
List Price:  C$5,170.09
You save:  C$1,477.27
रु13,360.74
List Price:  रु18,705.58
You save:  रु5,344.83
S/376.13
List Price:  S/526.61
You save:  S/150.47
K381.31
List Price:  K533.85
You save:  K152.54
SAR375.08
List Price:  SAR525.14
You save:  SAR150.05
ZK2,557.08
List Price:  ZK3,580.02
You save:  ZK1,022.93
L466.85
List Price:  L653.61
You save:  L186.76
Kč2,369.96
List Price:  Kč3,318.04
You save:  Kč948.08
Ft37,031.68
List Price:  Ft51,845.84
You save:  Ft14,814.15
SEK kr1,094.85
List Price:  SEK kr1,532.83
You save:  SEK kr437.98
ARS$86,967.11
List Price:  ARS$121,757.44
You save:  ARS$34,790.32
Bs693.40
List Price:  Bs970.79
You save:  Bs277.38
COP$389,858.15
List Price:  COP$545,817.01
You save:  COP$155,958.85
₡50,274.61
List Price:  ₡70,386.47
You save:  ₡20,111.85
L2,477.26
List Price:  L3,468.26
You save:  L991
₲742,412.64
List Price:  ₲1,039,407.40
You save:  ₲296,994.75
$U3,872.48
List Price:  $U5,421.62
You save:  $U1,549.14
zł405.79
List Price:  zł568.12
You save:  zł162.33
Already have an account? Log In

Transcript

Hello guys, welcome back to the next tutorial. In this one, we're gonna continue our discussion after the this keyword. So we're gonna learn about constructors. And there's something really interesting. So let's get started quickly, what I did is I just copied the same code from the this keyword. And this is just a very small class that we saw in the previous lecture.

And what it's doing is just creating an object from the car class and just setting the make and again, printing what the make is right? Here is something we have our car class, right. So let's talk about constructors now. So constructors, you can think of them as methods, but they're actually not methods. They look very similar to them. But then they're really not methods.

So don't don't get confused when we create methods, right? So it gets called on its own when an object is created. So when I'm creating an object of the class like C one, the constructor is going to get called on its own. On the other hand, for the methods, we have to make explicit calls, right? We may call sex even dots that make or any other method we have to make explicit cards. But this is not the case with constructors they get called on their own.

We don't have to make any explicit cards right. One more characteristic of constructor is that they hold the same name as the class. Unlike methods, right methods can have any other name, but the constructor constructors will have the same name as the class. So so let's go ahead. Right now we don't see any constructor in this class, right? But yes, there is there is a default constructor which is hidden, that's taken care of by Java, and it always exists and it doesn't have any arguments.

That constructor is known as no argument constructor, we can call it as like that. So let's create our first first constructor. So I'm going to say public car and parenthesis as we do in methods and these curly braces. So this is the default, no argument constructor and it also always exist. So what it does is it doesn't change anything in our code, we still create the object like this. Sorry, we still create the object like this and there is no change, right?

And it's just gonna work fine. It's just gonna work fine if I run this code and it still says BMW right? It what we are doing is just to rephrase everything, we have said make method which sets the make of this car. And the make is here, define here private string make. And the get method just returns make make is again, the make variable and it just returns this make. So in our constructor demo class, we're just creating an object.

We are first setting the make and then we are just printing out the make. So we're calling c one dot set, make and then we're calling c one dot get me right. So the constructor didn't change anything, right? So this is the by default constructor. Now there could be multiple constructors inside a class and they all can take different number of arguments inside the parenthesis, these parenthesis when I say parenthesis these ones, right. Like the methods, they can exactly take arguments like the methods like we do here, we have provided argument to a method called string make, we can provide arguments to the constructors as well.

And constructors are most usually they're used for initializing instance variable values, like the one we have. Private string make, we generally use constructors to initialize them. Like we can use the set make method to change the value or to replace the already existing value. We can do that but usually, the default value or just to initialize we, we pretty much use the constructors for those kind of things. So let's go ahead and do that. Let's I'm just gonna define couple of more variables.

So let's say in maybe, speed, right, so and then end and then I'm gonna say get So let's say I have two more variables, right. And I want to set some value to them right now by default, when I create a create object here, I want some default value to be set for speed and gear of my car. So usually what happens guys, usually speed is zero when we just had the car, we haven't pulled in anything. And the gear is also zero, right? So let's put these things. So what I've done is I've just put these two things inside my constructor.

So when I create an object of my class, I'm going to get these two values by default. And let's use this key word because that's what we have learned in that this key word lecture, right? Let's use this key word. So what's going to happen right now? So I have already created the object. It exists here, right?

So let's print out some value, some interesting stuff. So what I can do is I can just do that on care, right? And I can do See when? Da speed, right? And that's it. That's these are the two variables we defined.

So let's just put them in order, since we have it speed on the top. So now let's run the code. So I'm just going to run it. And you guys can see here, right? BMW 00. So 00 is this P. And you know what, just to make sure that this is happening, what I can do is, see so I'm gonna say eggsy cuting.

Construct car without arguments. And why I'm saying that because I'm not providing any arguments to this constructor, right? So if I run this code again, you guys can see that as soon as the object was created. Car see one new car right. The first line got executed executing constructor without arguments, everything else. God print after Afterwards, right?

So even this thing, see when a third make even these things, see when that's speed gear, everything was printed afterwards. The first thing that got printed was executing constructor without arguments. So because as I said, as soon as we create an object, the constructor is called. And it's it just triggers up all these by default values, whatever we want to set by default, it just triggers everything. So you can think of this as kind of a board up thing, right? And when we turn up the comm turn on the computer, there are some board processes and all those things.

So you can think of this thing like that. So this is what a constructor is. So let's create few more constructors in our class. So let's say at some point I want to create my object using some other variable so right now I'm just putting default values zero and zero for speed and gear. So let's say I want to I want to change these things. I can say speed And yeah, right.

So now this thing becomes another type of constructor. Now it is expecting arguments. So I'm just gonna say this dot P equals to speed of this one here. And this dot speed equals to OSI unspayed. Again, let's forget. So this got care equals to care of this thing again, right?

So when I do that, and let's print this stuff out, so you guys can differentiate it. So executing constructor where the arguments this time. So what I can do guys here is, now I have an object, which is the default constructor, which doesn't accept any values, we don't give any values to it. Now what I can do is, let me just print out so you guys can see the difference. So let's print some stars. And after that, we're going to create a new object.

So now I'm going to create a video basically Car c two equals new car. And this time, I'm not gonna put the empty parenthesis I'm not gonna call the default constructor. I'm gonna call this constructor. If you hit control space, you're gonna get these options. So this is the default one. This is the first one.

And then this is the one the second one, you guys can see car in speed in gear, that's cover I'm gonna call right. And for the speed, I'm just gonna say 10. And maybe let's put it in the first gear. So that's the solid can do right first gear can do up to the speed of 10. Right? So that's what I'm calling right now at this fine.

So now let's do something interesting. Now what I'm gonna do is I'm just gonna print the same thing. I'm just gonna print out the same stuff that we were printing, instead of seven. I'm just gonna put through and instead of the one, I'm gonna put two. So let's see, for the second object that we created, we should have different values, right? So let's, let's just say the code Planet.

So what I'm seeing here, the first is executing constructor without arguments, BMW 00, and then executing constructor with arguments. And it says here is 10. gears one, right? That sounds something interesting, guys, that's what constructors is. So you can set default values for your instance variables. And you can have different default values for different objects. This is how constructor works.

One last thing that I want to add in this tutorial is that you can call a constructor from within a constructor using the this keyword that we already talked about. So let's say foreigner call this constructor every time and when I create an object of default, I mean, that's not a very great use case. Because if I want to create an object without any default values, without even providing the default values, then I want to do that right. But let's see. Let's just imagine I'm doing something where and our call this constant Dr. Inside this constructor, so what I can do is I can just put this, and in parenthesis, I can put the value, I can put on a sphere as maybe, maybe let's put 10 and put the gate as two.

So once I do that, and if I run this code, so let's see that output for the first one. So let's run it. So it says executing constructor with arguments, right? Because we have this air, the argument, but we're calling inside it. And then it says, executing constructor without arguments, right? So that's what is happening guys.

So we can call out constructor from within a constructor, and it's still showing you zero and zero because afterwards we set it to zero and zero. So that's good, isn't it again becomes zero. So that's pretty much it guys. I would say practice a lot. That says the key to success. And let me know if you have any questions.

So it may sound silly, but When you practice only then you're going to get this thing. So thanks for watching guys and stay tuned for the next lecture. Thanks, guys.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.