A method is a series of statements combined into one block inside a class and given a name. In the Cold War days, these were called sub-routines, and many other languages call them functions. However, the main difference between a method and a function is that a method has to be associated with a class, whereas a function does not.
Call Me
Void In Java, void means that although the method might do many things, no result is returned.
Non-Java
Break It Down
Methods also exist to organize your code. One rule of thumb is to never have a method that is longer than one screen. It makes no difference to the computer, but it makes all the difference to humans (including you).
It’s also very important to name your method well. For example, a method that fires an arrow should be called “fireArrow,” and not “fire,” “arrow,” or “arrowBigNow.”
This may seem like a simple concept, but you might be surprised by how many people fail to grasp it. It also might be overlooked when you are in a hurry. If you don’t name a thing well, it will make your life (and that of other programmers working with you) harder in the future.
Return to Sender
Once the return statement is reached, the method is complete. Whatever code called the method will resume execution. If there is a return type (like the preceding Dragon), the method can return a value of that type and it can be used by the calling code (the preceding method returns a new Dragon object ).
Static
In Java, a static method is a method that is not linked to an object instance. It cannot refer to non-static fields of the class it is defined in. However, it must be part of a class.
For example, the random() method in the java.util.Math class we learned about earlier is a static method.
For example, if the preceding method is defined in a class named Fight, it could be called from another class as Fight.getWinnerBetween(dragon, vampire) where dragon is an instance of a Dragon and vampire is an instance of a Vampire.
Because Java is an object-oriented programming (OOP) language (as well as Scala and Groovy), static methods should be used sparingly, because they are not linked to any object instance. However, they can be useful in many circumstances. For instance , they can be useful for “factory” methods (methods which create objects). The method makeDragonNamed() defined previously is a good example of a factory method. Static methods can also be useful for code that is used from many different classes; java.util.Arrays.asList() is an example—it takes any number of parameters and returns a new List containing those values.
Varargs
Main Method
In the groovyConsole, just press Ctrl+R.
Right-click the Main class.
Choose Run File.
Exercises
Try out methods. After you’ve created the Main class, try adding some methods to it. Try calling methods from other methods and see what happens.
Lists, Sets, and Maps In Java, all of these data structures are under the java.util package. So, start by importing this whole package:
1 import java.util.*;
Then go back to Chapter 5 and try out some of the code there.
Summary
This chapter explained the concept of methods and how they should be used.
We also put together everything you’ve learned up to this point and made a small Java application.