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 x, int y){ //code to set coordinates goes here }
We could then use our new method from another part of our code, like this:
//I like it here
setCoordinates(4,6);//now I am going off to setCoordinates method
//Phew, I'm back again - code...