Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you declare own custom methods in your Java code, you can declare them with arguments, parameters, or values that they can receive and these methods can return values as well. I am starting in a version of my calculator application that's in a project called MethodWithArgs. This application already has a custom method that I previously used in other exercises. The name of the method is getInput. It's designed to receive a single argument, a string with a name of prompt and it's designed to return a string as well.
All of the complex code is encapsulated within the method. So, when I call the method, all I need to do is pass the name and a value that I want to display on the screen, the prompt. Now you can create your own custom methods incredibly easily in Eclipse. Let's say that you have some custom code and it's either 3 line or 10 lines or 100 lines of code, and you want to take that code out of the main method and make it its own custom method. Eclipse calls this extracting a method and here is how you do it. I am going to take these three lines of code, I am declaring two double values that are being parsed from strings.
And then I am adding the two values together. I would like to take this bit of code and make it its own method. So I'll select those three lines of code and then I'll right click in the selected area. I'll choose Refactor, Extract Method. Eclipse asks for the new Method name, I'll call it addTwoValues. Notice the parameters are set as Strings, that's correct, and also, Eclipse is smart enough to know that if my final statement is doing a calculation, then I'm probably going to want to return that value from the method.
So it says, I'm going to return a double value, I'll click OK and Eclipse refactors the code for me. The new code is a single line that says create a double value named result, which is calculated by calling the new addTwoValues method and it's passing in the two strings. And here is the new method, it's declared as private, static, and double, because it's private and static, it can be called from within the Main method, and because it declares double as the return type, it knows that it's going to return that calculated value.
Here is all the code that I extracted, and here is the code that returns the result. The keyword return means here is the value that I'm returning from this method and the data that you pass back must match the data type that you declare here. I'll Save my changes and Run the application, I'm asked for my values and I'll type in 5 and 10 and I get back 15. So I know that the code is working. It's also possible to declare methods that receive variable numbers of arguments, as long as all those arguments are of the same data type.
Here is an example, I moved down to the bottom of the application and I'll create a new private static method and I'm going to set the return data type as double, just as before. I'm going to call this method though addMultipleValues. I'll start with no arguments, but then I'm going to place the cursor inside the parentheses and I'm going to tell Java, you can accept any number of arguments as long as they're all doubles, that is, as long as they are all numeric primitives they can be translated as double. Here is the syntax: double space dot, dot, dot space, and then the variable name.
The data type works as it does for all arguments, but the dot, dot, dot means treat this value as an array. Right now, I'm seeing a little bit of error information and that's because I haven't returned a value yet. So now I just need to fill in the implementation of the method. I'll expand the editor so we can see more of the code, and within the method I'm going to start by declaring a double value of result and set it as 0d, meaning the double version of zero. Now I'm going to loop through those values, because of the dot, dot, dot in the middle of the declaration, values is passed in as an array, and I'll loop through using a for-loop, I'll type for press Ctrl+Space and I'll use a for-each loop.
Eclipse tells me it's going to create a new double value named d, each time through the loop, and this is the collection or array it's looping through, values. Within the loop I'll add the value to the result using the += operator result += d and finally after the loop I'll return the result. So, there is my completed method accepting variable numbers of double values. I'll go back to the top to the Main method, and I'm going to add a little bit of code down here, I'll say double resultOfMultiple = addMultiple and I'll pass in 1, 2, 3, 4, 5.
And again, I can pass in as many values as I want, as long as they can all be easily converted into double values. Then I'll output the result saying (" The answer from multiple values is") and then the answer. I'll run the application I still have to go through the inputs for the original calculation, but then I get the answer from the multiple values also. Now it just so happens that I got back the same values, so let's try running the application again. This time I'll add 5 and 5 together and I get back 10 for the first answer, but 15 for the second, because that's the result of 1+2+3+4+5.So this is how you declare your own methods with arguments.
Java requires arguments to be declared if you haven't declared them you can't pass them in and if you do declare them you have to pass them in, it's very strict. But you do have the option of using the variable argument syntax to allow yourself to pass in multiple arguments as long as they all follow the same data type.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101658 Viewers
61 Video lessons · 88415 Viewers
71 Video lessons · 72249 Viewers
56 Video lessons · 103984 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.