Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Nearly all programming languages support the concept of a function. A function is a set of code that's wrapped up and then given a name, so that it can be called easily from other parts of the application. In Java, we have special name for a function called a method. The term method comes from object- oriented vocabulary and refers to a function that's a member of a class. There are some programming languages in which you can create global functions. Functions that are available to the entire execution environment without having to say where they come from.
That's not the case in Java. In Java, every function is a member of a class, and so we always use the term method. I have described the use of the main method in applications that run from the console. As you start up an application from the command line, the Java Virtual Machine looks for a method called main, which has these characteristics, public, static, and void, and accepts an argument that's an array of strings. You can create as many of your own custom methods as you want to, but again, every method must be defined within a class.
I am going to define my own custom method and describe each part of the declaration as I go along. When you declare a method, the first thing you typically provide is called the access modifier. You can use one of three terms, public, private, or protected. Public means that the method is available to the entire application or at least to any part of the application that can see the class itself. So if the class is public and the method is public, then it can be called from anywhere. Private is the opposite. The method is only available to code within this class.
Protected has to do with inheritance. A protected method is available to the current class and to any of its sub classes. And there is one other kind of access, which happens when you don't declare an access modifier at all. It is sometimes called protected package, and it means that a method is available to the current class and to any other classes in the same package or group of classes. I recommend always setting access modifiers though, because it makes your code easier to read. The next characteristic of a method is whether it's static or not.
If you want the method to be static, you put in the word static. And if you don't, you don't. When do you want a method to be static? Well, the term static means that the method is something we call a class method, as opposed to an instance method. A class method is called directly from the class definition, whereas an instance method is called from an instance of the class or an object. If you are not sure whether to make a method static or not, try not putting in, and see if you can call the method. For example, in order to call a method from the main method, which is static, the called method must be static as well.
Static methods can call static methods, instance methods can call instance methods, without having a say where they are coming from. But for a static method to call a non static method, it must create an instance of the object. I want to call this method every easily, so I'll make it static and then I'll be able to call it from the static main method. Next, you declare the return type. The term void means I am not returning anything. Unlike some languages, Java requires a declared return type. Now you type in the name of the method you want to create.
I'll name my method doSomething, it's important to follow the right conventions when you name your methods and variables. You follow the same convention as for variables. The initial character must be lowercase or other java developers will think you don't know what you are doing, and it must be an alphabetical character, not numeric. You can also use underscores, but that's fairly in frequent. The rest of the method name can use camel case, such as I am doing here which means using uppercase characters to make the string more readable. At the end of the method name you always add opening and closing parenthesis.
This is where you would declare arguments that can passed into the function or method. After the parenthesis, you add up pair of braces. In Eclipse, if you type in the opening brace and press Enter or Return, the closing brace will be created for you. Now I've declared this method as void, meaning I am not going to return anything, but I can do something within the method, and I'll output something to the console saying ("This method has been called.") Now I'll go to the main method and I'll call the new custom method. I'll type do and then press Ctrl+Space to see what's available and there is my new method.
Eclipse is constantly scanning your code to determine what's available to the code and if it can't call a particular method, say because you are in a static context and the method you are trying to call isn't static, it won't show it in the list of choices. I'll press Enter or Return to choose the method and add the semicolon at the end of the statement, and I'll Save and Run the application, and there is the output. Now it's very common to create a bunch of code in a general context and then want to be able to wrap that up and turn it into its own method later on. We call this sort of changed to your code, refactoring, and it is something that Eclipse can help you with.
I am going to create a little bit of code here. I'll create an int variable called top and give it a value of 10, and then I'll add for-loop. I'll type '-for, press Ctrl+Space and then Enter or Return to iterate over the array. I'll press tab and I'll change the value that I am iterating on from args.length to top and then within the for-loop output System.out.println, and then the value is, and then I'll append i. I'll Save and Run the application and there is my finished code.
Okay, let's take this code and wrap it up in its own method. I am going to select the code that I want to put in to its own method, and then I'll go to the menu and choose Refractor>Extract Method. The Extract Method dialog asks me what I'd like to name this new method and I'll call it loopMe. It will propose what we call a method signature. The combination of the characteristics private, static and void, and the method name. I can change my access modifier up here or I can accept the proposal of private.
I'll accept the proposal. I'll click OK and Eclipse takes all of that code and moves it off into its own separate method. It also adds a call to the method right here. So I'll Save the changes and Run the application again, and I'll see that the application is doing exactly the same thing as before, but now I have wrapped up all that custom code into its own method, so it can be called many times in the future. So that's a look at the basic of creating methods. Now in future videos I'll show you how to create methods that accept arguments or parameters and also methods that know how to return values.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97366 Viewers
61 Video lessons · 84618 Viewers
71 Video lessons · 68815 Viewers
56 Video lessons · 101285 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.