Java Fundamentals

Java Fundamentals Greet User & Overloaded Methods

Learning Paths:
Check out a free preview of the full Java Fundamentals course:
The "Greet User & Overloaded Methods" 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 two methods to get a user's name and greet that user and discusses the function of the main method. Methods must have a unique parameter list and can be overloaded if they share a signature.

Get Unlimited Access Now

Transcript from the "Greet User & Overloaded Methods" Lesson

[00:00:00]
>> So we are going to write a program that, has a method it asks the user for their name. And then we'll have another method that greets the user by that name, all right? So let's go back to our IDE and we're going to create a new package called Methods.

[00:00:24] And we'll add a new class here called greetUser. Side of here we'll make our main, all right? So main is a method. We've been using that all along, all right? So let's go ahead and we're gonna say tring name equals =, and instead of me doing everything inside of this main method, I'm actually just going to make a call to a new method that we've not written yet, and we'll write that method.

[00:01:01] So we'll say getUserName,okay? Let's go ahead and write the getUserName method. So we'll say this is public, we'll make it static. So you can go ahead and just call it. It's going to return a string, which is the user name. And then the name of the method is getUserName.

[00:01:28] Parameter list, I don't need any parameters. So we'll just leave that empty. And then we have our set of curly braces to define our body, okay? So inside of here is where I can then do my output in my input so we'll go ahead and just basically, ask the user for their name, Enter your name.

[00:01:56] And let's go ahead and scan it in. And we'll store it inside a variable. So we'll say String name equals scanner.next, let's close our scanner, and then this is complaining because I have not fulfilled my obligation, I said I was gonna return to string, I have to do so.

[00:02:36] So to return, I write the word return and then the value that I want to return back. All right, so here we're calling getUserName here, we're getting the person's name, and then we're gonna return it back to here and we're gonna store it in a variable. Now we'll talk about scope in a little bit, but I wanna point out now I have a variable on line eight called name, in a variable on line 14 called name.

[00:03:15] All these are not the same cuz they live in two different methods. So whatever I do in my method is my business, wherever you're doing your method and your business, they don't know each other, okay?
>> So you could refer to like an ID and multiple methods, but it's not talking about the same thing the whole time.

[00:03:32]
>> Yeah, it's sculpt to that method. All right, so yeah, I got the name, is that all I need? I don't need to print as well, right? So let's go ahead and say, greetuser. And we'll give it a name. So this way we exercise sending data to a method, all right?

[00:04:02] So we'll create this new method, public static. Greetuser doesn't return anything, it's just gonna print something to the screen. So what's the return type for that?
>> Void.
>> Void, okay, and greetUser is the name. It does need to accept a name. What should the datatype be?
>> String.

[00:04:27]
>> String, and then we can call it whatever we want. We can call it name again. This now belongs to this method, right? Okay, so from here we'll just print out the name. We'll say hi there, and then put the name. Since it has a void, return type, we don't need a return statement, yeah.

[00:04:53]
>> At the very top with your main method. What's going on with the inputs with the string double brackets args?
>> Here?
>> Yeah.
>> Okay, so the main method this can be run from the command line, it can even be run using IDE settings, but we could send it some initial flags of data to say how we want this run,right?

[00:05:20] So let's say you want it run with some initial input or just something like that, or some sort of flags like I want this run headless or whatever you can. Put whatever you want, but this main method, this is the signature of it that says the name is main, and it takes an array of strings.

[00:05:42] So that's how you define it. In our case, we're not sending any data, so the array is empty. But if you were to run this from the command line, you could do that and specify a bunch of data.
>> Does it have to be an array of strings, could you send some kind of other input?

[00:05:58]
>> It has to be that because that's how main has defined itself, as saying I only take an array of strings. Okay, all right. So we called a method that did not take any parameters. But, it returned a value, and then we called a method that does take a parameter, and it does not return any values.

[00:06:23] Shall we run this? Okay, so, go ahead, enter your name here, and I got, Hi there, Angie. So for my main method, notice I'm now using main as a control mechanism to say, okay, I'm gonna call this method and then I'm gonna call this method and do X, Y, and Z, versus dumping everything into one method.

[00:06:50] So you want your methods kind of isolated and focused on a specific task, not like too big and doing three or four things all in one. All right, so you can have multiple methods with the same name, but remember, the methods are uniquely identified by their signature, and a signature consists of the method name in the parameter list.

[00:07:16] So it's legal to have things with the same name, but in order to still be uniquely identified, the parameter list must be different, all right? So here's an example of a class called Month. It has two methods, one that says getMonth and another one that says getMonth. So the first GetMonth method, it takes a string callMonth.

[00:07:41] The second one takes an integer callMonth. So this signature is different because they both take different types of data. Their parameter list is different, all right? So this is two unique signatures, getMonth string, getMonth int. Again, the name of the variable is irrelevant. So notice both of the names are month on the parameter.

[00:08:08] That's irrelevant, even if you were to have this third method here. It's another overloaded method where I say getMonth again, I take a string but its name is monthName, this will not compile. Why? What does it conflict with?
>> The second one, the one on line seven.
>> Why?

[00:08:32]
>> Same signature.
>> It has the same signature getMonth string. The variable name is irrelevant. We don't care what you name it, if I wanna call getMonth and they both take a string, I'm confused as the caller as to which one of these I'm supposed to be calling, right?

[00:08:49] So they're conflicting.