Methods are used to break complicated programs down into easy to handle chunks. They can take in data, process it and return results back to the main program. There are many advantages of using methods:
They make code easy to read
You can reuse code, even between programs
Multiple people can work on the same project at the same time
Design
Modifier:
public: indicates that the method can be accessed from anywhere
static: you can call the method without first creating the object to which it belongs
Return Type:
Methods can return data from up to one variable
Options include: int, double, String, boolean and void (no return value)
Method Name:
Follows the same naming rules as variable names
Parameters:
The variables sent to and used by the method
Variables declared in a method are separate from the main program:
o Methods can use new variable names or reuse names from the main
program
o Changes made to variables in a method do not change their values in the
main program
You need to use round brackets even if the method has no parameters
Example 1
Example 2
Comments