In Java, A method is a collection of statements that are grouped together to perform an operation.It is used to achieve the reusability of code. We write a method once and use it many times. We do not require to write code again and again. It also provides the easy modification and readability of code, just by adding or removing a chunk of code. The method is executed only when we call or invoke it.
In a simple word the primary uses of methods in Java are:
1. It allows code reusability (define once and use multiple times).
2. It increases code readability.
3. You can break a complex program into smaller chunks of code.
Syntax:
returnType methodName(){
// Method body
}
In the above syntax
returnType -> Method may return a value, For example if a method has an int return type then it returns an integer value.
If the method does not return a value, its return type is void.
methodName -> This is the method name. The method signature consists of the method name and the parameter list.
methodBody -> It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces.
For example,
int sum(){
// code to be executed
}
In this, The name of the method is sum(). And, the return type is int.
Method with parameter
Here is the syntax of parameterised method:
Syntax:
modifier static returnType methodName (para1, para2, ...) {
// method body
}
Here,
modifier -> Modifier or access specifier is the access type of the method. It specifies the visibility of the method.
static -> If we use the static keyword, it can be accessed without creating objects.
para1/para2 -> These are values passed to a method. We can pass any number of arguments to a method.
To call a method in Java, write the method's name followed by two parentheses () and a semicolon;
Following is the example to demonstrate how to define a method and how to call it.
class Simple {
// create a method
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
return sum;
}
public static void main(String[] args) {
int num1 = 25;
int num2 = 5;
// create an object of Simple
Simple obj = new Simple();
// calling method
int result = obj.addNumbers(num1, num2);
System.out.println("Sum is: " + result);
}
}
Output:
Sum is: 30
A Java method may or may not return a value to the function call. We use the return statement to return any value. For example,
int sumOfTwo() {
...
return sum;
}
Here, we are returning the variable sum. And return type of the sumOfTwo() is int. The sum variable should be of int type. Otherwise, it will generate an error.
class Simple {
// create a method
public static int square(int num) {
// return statement
return num * num;
}
public static void main(String[] args) {
int result;
// call the method
// store returned value to result
result = square(6);
System.out.println("Squared is: " + result);
}
}
Output:
Squared is: 36
A method parameter is a value accepted by the method. As mentioned earlier, a method can also have any number of parameters.
For example:
// method with two parameters
int addNumbers(int x, int y) {
// code
}
// method with no parameter
int addNumbers(){
// code
}
If a method is created with parameters, we need to pass the corresponding values while calling the method.
For example:
// calling the method with two parameters
addNumbers(5, 10);
// calling the method with no parameters
addNumbers()
class Simple {
// method with no parameter
public void display1() {
System.out.println("Method without parameter");
}
// method with single parameter
public void display2(int x) {
System.out.println("Method with x single parameter: " + x);
}
public static void main(String[] args) {
// create an object of Simple
Simple obj = new Simple();
// calling method with no parameter
obj.display1();
// calling method with the single parameter
obj.display2(20);
}
}
Output:
Method without parameter
Method with a single parameter: 20