Java Lesson 9 | Using Multiple Class part 2
Hello, this is Jake with Mac Heads 101. It's going to be another Java tutorial, and this will be my second installment of using multiple classes.
So, in this tutorial, I'm actually going to be building a program that we built before, which is where you enter your name, and then it says "Hello" to whatever they entered. But we're going to be doing so using multiple classes.
So, first, let's just get all the easy stuff out of the way. Get the Scanner, right? Give them a prompt to tell them to enter their name: "Enter your name," and a string variable to store their name, a Scanner object, and set that equal to the input.
All right, so now we got all that out of our way. I'm going to be showing you what we're going to be doing here. I'm going to make a set method and a get method. The set method is going to set the value of the name, and the get method is going to be to get that value.
So, um, public void. Void means it doesn't return a value. Public means open all classes. Went over that in the last tutorial. Set name, and this is the important part that I didn't go over: it takes string name.
Okay, so when I do put string name in here, it means when I call this method, I have to put a string variable in here, and it's going to take the place of name. I'm going to be using this string name in here, and whatever they put in here will take the place of name.
So, right now I'm going to make a string called word, and you know, I'll set it equal to nothing right now, and word equals name. So what this does is when I call this and I'm going to put name in there, which is a string, it's going to take this and set name equal to this string word.
Now, public string. This does return a value; it returns a string value: get name, no parameters, return word. So what this means is it returns a string, so I can then print out like if my object for the second class is called S, I could print out S.do get name.
After I set the name, name, so I'll demonstrate— it’ll make a little more sense. So second S equals new second. All right, and so I do S.set name, and in there I put... yeah, I called it name, and so it's already there.
But yeah, um, so what this is going to do is name is equal to their input. So the input equals word; word is then set to their input, so then it returns word. So it's going to be returning their input.
So, um, I'm going to print hello plus S.G get name. Cannot type today. All right, so what I'm—this is going to do—I'm going to run over it one more time. We did all this stuff.
When I this before, name equals whatever they input, and then it's going to set whatever they input, which is name, equal to this variable word. This method is then going to return word, which has been set equal to their input, and this string name here, um, I could call it like string phrase right equals phrase.
All right, and doesn't matter this variable name; when I put name in there, it will take the place of phrase. So, word equals name, the value of name, which is the input, and it returns that and then it prints out whatever it returned: hello plus whatever they return—what it returned.
So, it does the same thing as one we built before, but it's just an easy way to demonstrate using multiple classes. So, I'm just going to run this and enter your name: Jake. Hello, Jake!
And I'm just going to explain um about Constructors very briefly, and which means I can um set when I create the object for my class. All right, so what that means is a Constructor is a method that allows us to set our variables when we create the object. So, you'll see what I mean in a second.
So, I'm going to delete the set method. I'm not going to need it. So public, and the Constructor has to have the same name as the class. So, public second, and that's going to take string name, and then word equals name.
Same exact thing, but now look what happens. Um, because this is a Constructor, when I create this object second S equals new second, I'm getting an error here because there's a Constructor that takes a string variable. So, if I put that in there, there's no need for a set method.
I did it with the Constructor, which is essentially taking the place of the set method when I made the object. So, I'm just going to run it to prove it does the same thing. There we go, and that's the S.G name: hello, J!
And it does the same thing, and um later on, I'm going to be doing more tutorials advanced on multiple classes, talking about things like inheritance and polymorphism.
Um, but I'm going to take a little break from doing the multiple classes for a little bit and be teaching about a couple more things, then I'll come back to it. So, just letting you know. See you.