Methods

So what exactly are Java methods? A method is a collection of variables, expressions, and control flow statements. We have already been using lots of methods; we just haven't looked inside any yet.

Learning about Java methods will be the last topic for this chapter before we get practical and use what we have learned to enhance our math game.

The structure of a method

The first part of a method that we write is called the signature. Here is a made-up example of a signature:

public boolean shootLazers(int number, string type)

Add an opening and closing pair of curly braces with some code that the method performs, and we have a complete method, or a definition. Here is a made-up but syntactically correct method:

private void setCoordinates(int ...

Get Android Game Programming: A Developer’s Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.