A method has a method header and a method body. The header of a method has the following format:
[Method Modifiers] ReturnType MethodName ([Parameter List])The terms in the square brackets [] are optional. Hence, at the least a method header must have the ReturnType and MethodName. Java defines method signature to be the name of the method and the parameter list (the order of the parameters in that list is important).
public static int max ( int x, int y )
Some of the modifiers that we have learnt of so far are public and static. A public method can be accessed outside of the class, if the class has also been declared public. The static modifier allows the method to be accessed outside the class without creating an object of that class. For example, all the methods in the Math class have been declared static. We access those methods by calling on the name of the class and the method name - Math.sqrt(d) or Math.pow (a, b).
If the method returns some value, then the ReturnType of the method is the type of the value that is being returned. For example, if the method returns an integer after the result of some computation, then the ReturnType of the method is int. Methods that do not return any value have a ReturnType of void.
In Java we use the convention that method names start with a small letter. If the method name is two words then the first letter of the second word is capitalized. Method names may not be unique but method signatures must be unique.
The Parameter List is optional. Even if there are no parameters, the method signature must have the open and close parenthesis (). The parameters declared in the method signature are known as formal parameters. The order, the types, and the number of parameters are important.
The body of the method is enclosed in {}. Another term for the body of the method is the implementation of the method. The body of the method defines the method.
If a method has a return type then the value gets assigned to a variable of the same type.
int a = 2; int b = 3; int localMax = max (a, b);You can also print out the return value without assigning it to a variable.
System.out.println ("The maximum is " + max (a, b));
A method that has a return of void is just called by name.
public static void printMax ( int x, int y ) { ... } ... ... printMax (a, b);
public static void swap ( int a, int b ) { int tmp = a; a = b; // a has the value of b b = tmp; // b has the value of a } int x = 4; int y = 7; swap (x, y); // x is still 4 and y is still 7
public static int max ( int x, int y ) { ... } public static double max ( double x, double y ) { ... }You want to overload the names of the methods only if they offer the same functionality. Both the methods max return the maximum of two numbers.
public class SomeClass { public boolean found; public static void someMethod () { int found = 1; ... } public static double someOtherMethod () { ... } ... }The scope of the boolean variable found can be referenced or accessed throughout the class. For example the method someOtherMethod() can access the value of the boolean variable found. However, in the method someMethod() found is of type integer and has a value of 1.