
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
As programs grow larger, writing all code inside the main() method becomes difficult to manage. The same logic may need to be used multiple times in different parts of the program. Copying and pasting code leads to duplication, makes programs harder to maintain, and increases the chances of errors.
To solve this problem, Java provides methods. A method is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you can place it inside a method and call that method whenever needed.
In this chapter, you will learn how to create methods, pass data to methods, return values from methods, and organize code more effectively.
A method is a named block of code that performs a specific task.
Examples of tasks a method might perform:
Once a method is created, it can be called whenever that task needs to be performed.
Consider the following program:
System.out.println("Welcome");
System.out.println("Welcome");
System.out.println("Welcome");
If the same logic is needed many times, repeating code becomes inefficient.
Using a method:
displayMessage(); displayMessage(); displayMessage();
The code becomes cleaner, easier to read, and easier to maintain.
Syntax
returnType methodName() {
}
Example:
public static void greet() {
System.out.println("Welcome to Java");
}
Here:
public specifies the access level.static allows the method to be called from main().void means the method does not return a value.greet is the method name.For now, focus mainly on the method name and the code inside the method.
Creating a method does not execute it.
A method must be called.
Example
public class Main {
public static void greet() {
System.out.println("Welcome to Java");
}
public static void main(String[] args) {
greet();
}
}
Output
Welcome to Java
Explanation
greet() is created.main() calls the method.Example
public class Main {
public static void greet() {
System.out.println("Welcome");
}
public static void main(String[] args) {
greet();
greet();
greet();
}
}
Output
Welcome Welcome Welcome
A single method can be called as many times as needed.
Sometimes a method needs data to perform its task.
This data is passed using parameters.
Syntax
returnType methodName(parameter) {
}
Example
public static void greet(String name) {
System.out.println("Hello " + name);
}
Calling the method:
greet("Rahul");
Output
Hello Rahul
Explanation
name is a parameter."Rahul" is passed to the method.name.A method can accept multiple values.
Example
public static void displayUser(String name, int age) {
System.out.println(name);
System.out.println(age);
}
Calling the method:
displayUser("Rahul", 18);
Output
Rahul 18
Some methods perform a calculation and return a result.
For this, the return statement is used.
Example
public static int add(int a, int b) {
return a + b;
}
Calling the method:
int result = add(10, 20); System.out.println(result);
Output
30
Explanation
result.Methods that do not return a value use the void keyword.
Example
public static void greet() {
System.out.println("Hello");
}
This method performs an action but does not return anything.
The return statement immediately ends a method and sends a value back to the caller.
Example
public static int square(int number) {
return number * number;
}
Calling:
System.out.println(square(5));
Output
25
Example
import java.util.Scanner;
public class Main {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
int result = add(num1, num2);
System.out.println("Sum = " + result);
}
}
Input
10 20
Output
Sum = 30
Explanation
add() method.Wrong
public static void greet() {
System.out.println("Hello");
}
The method is created but never called.
Correct
greet();
Wrong
public static int getName() {
return "Rahul";
}
The method is declared to return an int, but returns a String.
Correct
public static String getName() {
return "Rahul";
}
return statement.void methods do not return anything.