Introduction to Java methods
Java methods are a way of organizing and compartmentalizing our code. They are quite a deep topic and a full understanding requires knowledge of other Java topics. By the end of the book you will be a method Ninja but 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 like draw
, takeShot
, newGame,
and printDebuggingText
as well as a few more.
Code with a specific purpose can be wrapped inside a method, perhaps like this:
void draw(){ // Handle all the drawing here }
The above method called draw
could hold all the lines of code that does the drawing for 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 but for now, you just need to know that all...