What is a method?
A method is a block/group of code can be used to perform and accomplish a specific task. Methods allow us to write code more efficiently with less complexity. We have already used many methods in previous examples like – System.out.println(). The println() method is the predefined java method used to print a message on the console. Similarly, we can also create and use our own methods in the program.
What we can achieve with methods?
- We can avoid duplication.
- Allows reusability of code.
- Reduces the complexity of the program.
- Allows us to write code in a modular fashion.
How to create a method
A method is consists of 6 components –
- Modifier – The modifier defines the access type of the method.
- Return type – The data type of the value if the method is returning any value otherwise void type.
- Method name – A valid method name.
- Parameter list – Comma separated multiple parameters or empty parentheses () for no parameter.
- Exception list – An optional exception list a method can throw.
- Method body – Body of the method, inside the method body we write our logic and codes.
Syntax
Following is the syntax for a java method –
<modifier> <return-type> <method-name> (<parameter list>) <exception list> { // method body }
Example
Following is an example method returning a sum of two numbers-
public int sum(int a, int b) { int c = a + b; // Return sum of a and b return c; }
To use the above method we need to call and pass two integer numbers in it, see the following example to better understand –
// Get the sum of 10 and 20 int resultA = sum(10, 20); // Get the sum of 10000 and 12345 int resultB = sum(10000, 12345);
We can call the method sum() multiple time as per our requirements.
Different examples
Following are the examples of methods try to run and modify the examples to better understand-
Void type method
A method not returning any value is considered as void type method. To create a void type method we use keyword ‘void’ in place of the return type.
public class MethodExample1 { // A void type method public void sayHello(String name) { System.out.println("Hello, " + name); } public static void main(String[] args) { // Create object of class Main MethodExample1 obj = new MethodExample1(); // Call the method obj.sayHello("John"); // Call the method again obj.sayHello("Rock"); } }
Return type method
A method can return any type of value at any point from inside the method body, but remember that method can return only once in its life cycle.
public class MethodExample2{ // A method to add two numbers public int add(int a, int b) { return a + b; } // A method to subtract number b from a public int subtract(int a, int b) { return a - b; } // A method to multiply number a with b public int multiply(int a, int b) { return a * b; } public static void main(String[] args) { // Create object of class Main MethodExample2 obj = new MethodExample2(); int num1 = 10; int num2 = 5; // Calling method add(int, int) int add = obj.add(num1, num2); System.out.println("Addition: " + add); // Calling method subtract(int, int) int sub = obj.subtract(num1, num2); System.out.println("Subtraction: " + sub); // Calling method multiply(int, int) int mul = obj.multiply(num1, num2); System.out.println("Multiplication: " + mul); } }
No parameter method
Sometimes we need to create a method without the need for any parameters, this type of methods are considered as no parameter method.
public class MethodExample3 { // Method without any parameters public void runLoop() { System.out.println("All items:"); for(int i = 1; i <= 10; i++) { System.out.println("Item " + i); } } public static void main(String[] args) { // Create object of class Main MethodExample3 obj = new MethodExample3(); // Call the method obj.runLoop(); } }
Static method
Static methods are generally used as utility methods in program, a static method can only use static fields and can only invoke other static methods. The Java main() method is a good example of a static method.
To call a static method we don’t need to declare and initialize objects, we can directly invoke it by its class name.
public class MethodExample4 { // A static method public static void runLoop() { System.out.println("All items:"); for(int i = 1; i <= 10; i++) { System.out.println("Item " + i); } } public static void main(String[] args) { // Call the static method MethodExample4.runLoop(); } }