Methods in java

 Methods in Java


Methods:
    (DRY)- don\t repeat yourself
        the method in Java is a collection of instructions that performs a specific task.

Creating Methods:

Public static int methodName(int a, int b){
	//body
}
The syntax shown above includes -

modifire It defines the access type of the method and it is optional to use.
returnType Method may return a value.
nameOfMethod This is the method name. The method signature consists of the method name and the parameter list.
Parameter List The list of paramethers, it is the type, order,and number of parameters of a method. These are optional, method may contain zero parameters.
method body The method body defines what the method does with the statements.



Example
public class Introduction {
    public static void main(String args[]) {
        int x = 34, y = 43;
        int result = maxOf(x, y);
        System.out.println(result + " is max");
    }

    static int maxOf(int a, int b) {
        if (a > b) {
            return a;
        } else {
            return b;
        }

        // return a > b ? a : b;
    }
}
43 is max
Method Overloading:
    When a class has two or more methods by the same name but different parameters, it is known as method overloading
Ex.

int maxOf(int a, int b){
}
int maxOf(int a, int b, int c){
}
int maxOf(float a, float b){
}