Java Fundamentals

Java Fundamentals Sending & Receiving Objects

Learning Paths:
Check out a free preview of the full Java Fundamentals course:
The "Sending & Receiving 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 demonstrates how to pass objects into methods and how to return objects from methods. Student questions regarding why getRoom() and calculateAreaOfRoom() are not static and if instantiating a class within its own main function is a common practice are also covered in this segment.

Get Unlimited Access Now

Transcript from the "Sending & Receiving Objects" Lesson

[00:00:00]
>> Objects can be passed into methods. They can be returned from methods. They can be basically used as we've done with all of our primitive data types already. They're just a little bit more powerful. They have more information inside of them, okay? So, we're gonna update the area of the rooms.

[00:00:19] We're gonna include a calculateArea() method that accepts two rectangles. And we're gonna also include a getRoom() method that returns a rectangle object. So you'll see how to pass objects, how to return objects, okay? I'm gonna make another class just so that you have this basic example. And then so inside of here, let's just clone this one.

[00:00:51] And we'll call this our redo, HomeAreaCalculatorRedo, all right? So, ooh, okay, good. This one gets into static, so everybody pay attention, okay? All right, so let's go ahead and make those two methods to start off with. So we're gonna say public, Rectangle is my return type. And the name of the method is getRoom(), takes no arguments, all right?

[00:01:32] So in this I can go ahead and ask for the length and the width. So instead of me hard coding those values, I can say, "Enter the length of the room" and then I wanna scan that in. So remember when we talked about with scanner or any object for that matter if I wanna use this in multiple places, there needs to be a global variable.

[00:02:08] So let's go ahead and define scanner here. And this time I'm not gonna make scanner static. I'm gonna show you what that means, all right? So we'll make this private. And I'm even gonna initialize it here, Okay? I need to import it, right? Because it's from another package.

[00:02:34] All right, so here into the length, I'm gonna read it in, double length = scanner.nextDouble(). Nope, nextDouble(), okay? And then same thing for the width. All right? So, I read them both in. Now, I wanna return a new object. So, I can just say return new Rectangle(). I have the length and the width, I might as well set them in the constructors.

[00:03:25] What was the order again? Hold on, Rectangle(). So notice that it shows me double length and then double width. So length and then width, okay? So, that's a method that's actually returning an object back. And then we wanna create a calculate area. So, let's say public, I guess double calculateAreaOfRoom().

[00:04:00] And this one will take two rooms, two rectangles. So Rectangle room1, Rectangle room2, right? So here, I can just do a return room1.calculateArea() +, I might not need this, room2.calculateArea(). Yeah. All right? And then we need to call it from the main method. So I'm gonna erase everything except that last print line.

[00:05:01] In this, all right. So we say Rectangle room1 is going to be getRoom(), right? We're gonna call it getRoom() method. So notice, I'm trying to call getRoom(). I have an error that says, Non-static method getRoom() cannot be referenced from a static content. So we talked about static a little bit, right?

[00:05:20] So when something is static, this main method is static, it means everything that we've used here or before here we've made static because we haven't set up state, we haven't called it into a constructor, or anything like that. Now, I'm in this class, I'm in the main method, which has to be static.

[00:05:41] I have no choice about that. But I wanna call into a method that's not static. Well, in order to do that, I have to create state, I have to create an instance of the class that this method lives within. It just so happens this method lives within the same class.

[00:06:00] It does not matter, I still have to instantiate this class in order to do it. So to do so, I would have to say HomeAreaCalculatorRedo, right? And we can just call this calculator = and I'm instantiating this class. Notice I do not have a constructor defined in this class but one is I called into one just fine because it's there by default.

[00:06:30] So after I have this calculator now I can use that object to call non-static methods. All right? And this really does depend on things that have to do with the class, right? I'm using this scanner object, which is a global variable. I needed to have a presence of this room, to have some sort of state set up, all right?

[00:07:04] So we have the first room. We can say room2 = calculator.getRoom() again. So it'll be same method but different values, right? It'll prompt them again. Okay? And then I want to call that other method, calculateAreaOfRoom(), so I can say double totalArea = calculateAreaOfRoom(). Yeah, I use my object for this class, calculateAreaOfRoom().

[00:07:46] I have to give it two rectangles, so I give it room1 and I give it room2. And now I can get that value back. Okay, let's run it. So this is the first room, right? Length, just say anything, okay? And then the second room, And then we got area of both rooms.

[00:08:16] So, we did it the same thing except we did it by using methods, passing and returning objects, okay? Is the static stuff clear? Not really?
>> I'm still a little foggy on the why we wouldn't make the getRoom() and calculateAreaOfRoom() static and then not have to instantiate a new class.

[00:08:40]
>> Which one? What line number?
>> So if we commented out 10.
>> All right.
>> And then removed our references to calculator and just called getRoom().
>> Yeah.
>> And then made those two methods static. What's the difference there?
>> Yeah, yeah, yeah. Okay, so say I made this one static, right?

[00:09:06] That would be fine. The problem is with this being static, now I don't have access to the global variables in this class. Because if we think about the class, how we set it up, the data, the methods, all of this, the object being all-encompassing of everything that it needs.

[00:09:23] Without an object, I don't have an all-encompassing. So the static is like, look, I make something static if it doesn't require using fields and methods that are all connected within this one objects class. I can use a static method to say something like, say for example, I wanna know is this a perfect square root or something like that, right?

[00:09:53] I don't really need access to anything else other than those numbers. So with static methods, you'll typically see where they'll wanna receive some data versus using data that would be within a class, right? So I could make a calculate area for rectangle that allow people to pass in the length and width.

[00:10:11] I could make that static, and that way you don't have to bother with instantiating this class, having an object. You could just say rectangle dot calculate whatever, right? Give it the information. But in object-oriented programming, the whole goal is to have the data, have the methods all within this object itself so you don't have to keep referencing this data every time you wanna use something in the class.

[00:10:36] You set it up one time, and you can call the behaviors as you wish without keep on giving the data over and over. So that's the benefit of not having something static. So you would use static when you don't really care about having state and everything associated with an object.

[00:10:54] You could just say the class name dot the method name and do whatever you wanna do.
>> And is this more because this is an example that is called HomeAreaCalculatorRedo? Is it common that you'd be instantiating the class inside of its own main function? Or is that more because it's an example?

[00:11:15]
>> It's because it's an example, really. Yeah, usually you wouldn't, necessarily. Well, it's because it's an example with the main method and that main method has to be static because this is the first method that's called.
>> Yeah.
>> So there's no object set up or anything. Java just needs to be able to call this method without state, right?

[00:11:41] So that's why this method is static. And so if you're gonna do something inside of your main method, then yes, you would have to instantiate the class that you're in, in order to be able to access these things or make them static as well. So that's why before we made everything static, because we hadn't gotten the objects yet, and instantiation, and state, and all of that.

[00:12:07] But yeah, in the real world, you're not really doing things in the main. There will be one class with the main that kind of kicks that whole application off. And then everything else, you're just dealing with your own classes and methods and you don't have to bother with this sort of thing much.

[00:12:22]
>> Cool, that's more clear. Thank you.
>> We should have used our bar here. I'm gonna show you. Remember we talked about bar? I'm gonna leave it there since that was our first time but next time I'm gonna whip out bar. All right, are we good? So we understand passing and returning objects within methods.

[00:12:46] Yes.