Introducing Java methods
Java methods are a way of organizing and compartmentalizing our code. They are quite a complex topic, and a full understanding of them requires knowledge of other Java topics. By the end of the book, you will be a method ninja. However, for now, a basic introduction will be useful.
Methods have names to identify them from other methods and to help the programmer identify what they do. The methods in the Sub' Hunter game will have names such as draw
, takeShot
, newGame
, and printDebuggingText
.
Note that code with a specific purpose can be wrapped inside a method; for example, take a look at the following snippet:
void draw(){ Â Â Â Â Â Â // Handle all the drawing here }
The preceding method, named draw
, could hold all the lines of code that draw our game. When we set out a method with its code, it is called the method definition. The curious-looking prefixed void
keyword and the postfixes, ()
, will be explained in Chapter 4, Structuring Code with Java Methods. However, for now, you just need to know that all of the code inside the draw
method will be executed when another part of the code wants it to be executed.
When we want to initiate a method from another part of the code, we say that we call the method. And we would call the draw
method with the following code:
draw();
Take note of the following, especially the last point, which is very important:
- Methods can call other methods.
- We can call methods as many times as we want.
- The order in which the method definitions appear in the code file doesn't matter. If the definition exists, it can be called from the code in that file.
- When the called method has completed its execution, the program execution returns to the line after the method call.
So, in our example program, the flow would look like this:
… // Going to go to the draw method now draw(); // All the code in the draw method is executed // Back from the draw method // Any more code here executes next …
Important note
In Chapter 8, Object-Oriented Programming, we will also explore how we can call methods to one file from another file.
By coding the logic of the Sub' Hunter game into methods and calling the appropriate methods from other appropriate methods, we can implement the flow of actions indicated in the flowchart.
Overriding methods
There is one more thing that you need to know about methods before you do some more coding. All the methods I mentioned earlier (for example, draw
, takeShot
, newGame
, and printDebuggingText
) are methods that we will be coding. They are our very own methods, and they are for our use only.
Some methods, however, are provided by the Android API and are there for our (and all Android programmers) convenience – we can either ignore them or adapt them. If we decide to adapt them, then this is called overriding.
There are lots of methods that we can override in Android, but one method is overridden so often that it was automatically included in the autogenerated code. Take a look at this part of the code again:
@Override protected void onCreate(Bundle savedInstanceState) { Â Â Â Â Â super.onCreate(savedInstanceState); }
In the preceding code, we are overriding the onCreate
method. Notice that the prefix and postfix to the name are quite complicated. Exactly what is going on here will be explained when we more thoroughly deal with methods in Chapter 4, Structuring Code with Java Methods.
Important note
The super.onCreate…
code will also be discussed in depth. But if you can't wait, here is a brief explanation: the super.onCreate…
part of the code is calling another version of the onCreate
method that also exists, even though we cannot see it. This is the one we are overriding.
Now we can add the method definitions to the Sub' Hunter code.