Java Fundamentals

Instantiating Objects

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

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

Angie walks through writing a class that creates instances of the Rectangle class to find the total area of two rooms in a house. Student questions regarding how the HomeAreaCalculator knows to reference Rectangle and if objects can contain other types of objects are also covered in this segment.


Transcript from the "Instantiating Objects" Lesson

>> So great, now we're going to instantiate an object. So we're gonna create our first object using that Rectangle class that we just made. So we're gonna write a class that creates instances of Rectangle to find the total area of two rooms in a house. So now you start seeing, okay, if I thought about this room that we're in right now, this is a rectangle, right?

So you start seeing how the real world can be modeled as objects within Java, all right? So let's go back and we're gonna create a new class, and we're gonna call this one HomeAreaCalculator. All right, so let's make our main, And we're gonna create an instance of the Rectangle class.

Now, to do this, it's like we did the scanner. Do the name of the class, that it becomes the type. So, so far, we've looked at primitive data types, int, Boolean, double, string, char, Rectangle or any other object can also be a type, okay? So this has become our type, and then it's a variable, we give the variable a name.

So you can name this whatever you want, I'm gonna name it room1. And then to instantiate the variable, that means, I mean instantiate the object, that means to go ahead and create the object, you're gonna use the keyword new. And then you're gonna call one of its constructors.

Remember if it did not define any constructor at all, the default one will still be available for you to use. We have two constructors, so we can use either one, let's go ahead and use the default one. And for room2, we'll use the other one so I can show you the difference between them.

All right, so now we have room1, and we've called into the constructor to instantiate it. So what's the length and the width of room1 right now?
>> Zero.
>> Zero, because that's what we said we were gonna do in our constructor. So using this object, now we have access to all of the behavior, all of the methods that are defined in the rectangle class.

So I can say room1., boom and we see everything that we've defined. So let's go ahead and set the length, and we'll set the length to 50, and we'll set the width, we can just set that to maybe 25. All right, and now I want to create the area of this room.

So I can say double areaOfRoom1 = room1.calculateArea. So now we got an object going, all right? Let's do the same thing for room2. So this time, we'll say Rectangle room2 = new Rectangle. And let's go ahead and call the other constructor with the length and the width. And that way, we won't have to do those method calls for the values.

So we'll say 30, maybe 75, Okay? And now, I don't have to call those setters anymore, so I can say room2.calculateArea, right? And let's go ahead and store that in a variable. So we'll say double areaOfRoom2 =. All right, so you see the difference between the two with the constructors.

>> First, when you're using setters [INAUDIBLE] you're setting it directly.
>> Yeah, so I use the default constructor, and then I use methods to set stuff up. And then because they did provide me with one, cuz I mean, every time you use an object, that specific object may not have the constructors that you need to set everything up the way you want it to.

So sometimes you may have to use one of its constructors and then add a little bit more to it or something to set its state the way you need it set. Or say, for example, I created this rectangle, and then I did input from a user. And then I wanna set it as I'm getting the input, so that allow me to be a little bit more flexible with how I set the object up, right?

So we want the prompt set to calculate the total area of both of these rooms, so we can just add these together. totalArea = areaOfRoom1, oops, I forgot my equal sign, + areaOfRoom2. All right, and then we can just print it out. Area of both rooms. All right, Amazing.

>> So you didn't have to import the Rectangle class because it's of the same package?
>> Yes, very good, very good. So I did not have to import Rectangle because it's in the same package. So if something is in the same package, Java can find it. If this Rectangle class was outside the package, and we'll try that later, like we did with scanner, right?

>> Yeah.
>> So scanner lives in another package, which is why we always have to import it.
>> But if you commented out package objects, it would suddenly not know anything about. Yeah, it will complain that I don't have a package declaration first, but let's just move, maybe move Rectangle just for a moment, you all don't do this.

Sure, oops, did I totally lose it?
>> Have you tried?
>> [INAUDIBLE] class.
>> I do, well, that's pretty neat. How cool is that? All right, that's not what I wanted to do. [LAUGH] Okay, let's just make a new Rectangle class. Okay, public. Okay, all right, and then when I come to home calculator, now it does not, crap.

All right, How do I say yes? Okay, [LAUGH] all right, now it does not, no Rectangle, right? It's like, I don't know what you're talking about. And then I would have to import it from methods. All right, so let's move that back. [LAUGH] Okay, nice. How do I close this whole thing?

Okay. All right, cool, any other questions about this so far?
>> Objects are just collections of fields and values kind of modeling a certain behavior. Objects, can they be composed of other types of objects? Let's say, as an example, you have a car that's a base, but then you might say a Ferrari or a Ford or whatever, is that a-

>> Yeah, so you could have more specific types of objects. So you could have, for example, we have this Rectangle as a generic thing. We could have created another class called room that was more specific and it could be an extension of the Rectangle class, right? And we'll talk more about that, I think what you were asking is, could you have an object within an object?

Yeah, you could do that sort of thing as one of your fields. So in the Rectangle field right here, I have primitive data types, doubles and ints, I could have another object as a data type, for example.
>> So you could have a shape object, where rectangles always have four sides, but a circle might not, for example.

>> Yeah.
>> You could say a rectangle is a kind of a shape.
>> Yeah.

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