Java Fundamentals

Constructors

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Constructors" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses constructors, which are structures within a class that are used to set an object's initial state. Default constructors and a student's question regarding if static classes have default constructors are also covered in this segment.

Preview
Close

Transcript from the "Constructors" Lesson

[00:00:00]
>> So constructors are also a part of classes. And what they do, they're a special kind of structure that's used to initialize the object, so set its initial state before you start using that object. And there's different types of constructors. So let's go ahead and talk about a few of them.

[00:00:27]
We're gonna add a default constructor and what's called an all-arg constructor to the Rectangle class. So in our ID, I'm gonna go back to the constructor. Now, this is also a convention. The constructors typically appear right after the fields and before the methods. Now, within a class, it doesn't really matter where anything is defined.

[00:00:53]
I could define my fields. I could switch the order of these methods. I could put constructors at the bottom of the class. It doesn't really matter. But convention sake, just so things are tidy, if you were to go into someone else's Java code, you wanna know where to look for stuff.

[00:01:12]
Usually have your global fields, your constructors, and then your methods, all right? So the constructor has the exact same name as the class. So you write an access modifier, typically public. No return type or anything, it's not a method. And then literally the exact same name as the class, so Rectangle.

[00:01:39]
>> Is this the same as overloading a method like you were doing earlier?
>> No, not quite. So the constructors, they look like a method, but they're not really a method. So their purpose is explicitly to set up the initial state of this object before you start using it.

[00:02:00]
So what you're gonna do inside of a constructor is set values to some of your variables. Or if you needed to, I don't know, open connections to something. It's basically a setup type of thing, right? So this one is called a default constructor because it does not take any arguments.

[00:02:22]
Notice there's nothing in the parameter list there, right? So what you're gonna do is say, okay, by default, what do I want my field values to be? So maybe you'll do something like set the length to 0, set the width to 0. The sides are already set to 4.

[00:02:45]
I'm good with that. So that would be an example of a default constructor, even if you do not explicitly define a default constructor. If I took this out, this class will already have a default constructor. You just can't see it. But by default in Java, the default constructor is already there.

[00:03:11]
It has no body, it doesn't really do anything. But all classes have a default constructor by default. [LAUGH] But if you wanted to actually do something, you would then go ahead and write an explicit one, okay?
>> So you're really just exposing something that exists under the hood to manipulate it.

[00:03:32]
>> And put in a body there, yes.
>> Would you usually call the setters or would you use this access from-
>> Yeah, so either way, it's totally fine. In fact, I'll do both just so you can see that they're both fine. So I'll set the length that way, and I'll set the width this way.

[00:03:56]
>> Is it more convention or is one way, I mean, there-
>> So if you have the setter methods, then it's best to use those. You kinda don't wanna be in the habit. Okay, so line 11 is exactly what I wouldn't wanna another class to do. I don't want them to set my variables just like this, call my methods and set it that way, cuz that's the proper protocol for how to deal with my fields, right?

[00:04:20]
So Rectangle is a constructor, it's in this class. Technically, it's okay. I can set the fields directly if I want to. But if you have those methods, it's best to just use those. Cuz I never know in the set method, maybe I might not just set this value, but maybe I need to update something else.

[00:04:41]
If this was a bigger project, setting something, maybe I set it in the program, but I also set some flag in a database or something like that. So there's multiple things going on, and it's best to put them all within a single method. So in the constructor, I would kinda miss all of that other stuff that was going on if I set it this way, yes.

[00:05:04]
>> Do static classes have a default constructor as well?
>> No, so [LAUGH] folks will not get off static. We'll talk about static, and we're getting into objects, so we're getting really close to get into the static part. But when we talk about objects and explicitly constructors, in order to create a new object, I have to call one of its constructors, all right?

[00:05:33]
With a static class, you're basically saying, and remember, what did I say the constructor does? It sets up state for that object, the initial values. With a static class, you wanna be able to access any of these methods without setting up state. I don't care about state. I don't need state.

[00:05:52]
I just need some statements executed. So in that case, you would not need to call a constructor, cuz you don't need any state set up, all right?
>> This might be skipping ahead a little bit, but I have a question about the constructors and kind of what you're doing there.

[00:06:11]
By setting it to zero, does that mean it's not, for example, starting with a null value like it would have otherwise?
>> Right, it would've been not defined, nothing there, right? All right, okay, so that's the default constructor. Let's also write the all-args constructor. The all-args constructor will take values for all of the fields that it has available, all of its, Those global fields.

[00:06:42]
So the name would still be the same, Rectangle. Again, remember when we talked about signatures. Names can be the same, so that's similar to overload, but then the parameter list has to be different. So we can say, okay, for this one, people can create a new object and they can pass in, what did we have?

[00:07:04]
The length, they can pass in the width. And then they can also, maybe you say they pass in the sides, right? Since it's a rectangle, we know that it has to be four sides, so that would be kinda sloppy to even allow them to. But since we're talking about all-args, that's what it usually means, all of the arguments, right?

[00:07:26]
Letting them pass all of the arguments in, but we're not gonna do that. [LAUGH] All right, so in this case, same thing. You're gonna set the length, but this time, you're gonna do it with whatever they passed in. And then you're gonna set the width, To the width that they passed in, okay?

[00:07:47]
So you're not making up a value this time. They actually gave you some values to use. Now, notice here, besides the sides, I don't really have values for any of this data, but I have a perfectly functioning class that I could calculate the perimeter. I could calculate the area, and I don't need to know what the values are until people wanna use it.

[00:08:13]
So once they start using it, they can put in any values that they want and then they have all of the behaviors here that they need to do whatever they wanna do.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now