Methods in Java
A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a sub-program that acts on data and often returns a value.
Each method has its own name. When that name is encountered in a program, the execution of the program branches to the body of that method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.
A method definition consists of a method header and a method body. Here are all the parts of a method:
Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.
Return Type: A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void.
Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.
Parameters: A parameter is like a placeholder. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.
Method Body: The method body contains a collection of statements that define what the method does.
Java Methods
There are four types of methods in Java.
void method without parameters
void method with parameters
non-void without parameters
non-void with parameters
The following is the Playlist for the above methods:
https://www.youtube.com/watch?v=o5JL5MrMHgs&list=PLIYIxEPySucxVCSJX6TioygD55ywZTcI1
In other words:
A simple Java method requires a minimum of three items:
1. Visibility : public, private, protected
2. Return Type: void, int, double, (etc.)
3. name: whatever you want to call the method
Visibility means who can access it. If it is public, anyone who has access to your class file can access the method. In some circumstances, this is perfectly fine. If the method is private, only the functions inside of that class can call the method. This is used as utility methods to do something you do not want just anyone who uses the class to do. Protected gives public function to all child classes.
Return type is void if you do not want the method to give you any data back. It would be used for such things as a method that prints out something. Any other return requires a return statement with the type of data it returns. For example, if you add two integers and want the results of that integer, your return type would be int.