A method in Java is a block of code that performs a particular task. You can call a method whenever you need it, which helps reduce repeated code and makes programs easier to organize.
Consider a program that prints a welcome message. Instead of writing the same code repeatedly, you can create a method.
public class Main {
static void greet() {
System.out.println("Welcome to Java!");
}
public static void main(String[] args) {
greet();
greet();
}
}
Output:
Welcome to Java!
Welcome to Java!
accessModifier returnType methodName(parameters) {
// Method body
}
| Part | Meaning |
|---|---|
| Access modifier | Controls where the method can be accessed. |
| Return type | Specifies the type of value returned, or void if no value is returned. |
| Method name | The name used to call the method. |
| Parameters | Optional input variables received by the method. |
| Method body | Contains the statements executed by the method. |
Create a simple method and call it from main().
public class Main {
static void display() {
System.out.println("This is a Java method.");
}
public static void main(String[] args) {
display();
}
}
Output:
This is a Java method.
The method is named display(). The statement
display(); calls it.
A method without parameters does not receive input values from the caller.
public class Main {
static void showMessage() {
System.out.println("Welcome to Soopro Pathshala!");
}
public static void main(String[] args) {
showMessage();
}
}
Output:
Welcome to Soopro Pathshala!
The empty parentheses in showMessage()
indicate that the method has no parameters.
Parameters allow a method to receive values when it is called.
public class Main {
static void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
greet("Amit");
greet("Rahul");
}
}
Output:
Hello, Amit
Hello, Rahul
Here, name is a parameter. The strings
"Amit" and "Rahul" are arguments
supplied when the method is called.
A method can receive more than one parameter.
public class Main {
static void studentInfo(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
studentInfo("Amit", 20);
studentInfo("Priya", 19);
}
}
Output:
Name: Amit
Age: 20
Name: Priya
Age: 19
The arguments must match the parameter types and order required by the method.
A method can calculate a value and return it using the
return statement.
public class Main {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println("Sum = " + result);
}
}
Output:
Sum = 30
The method returns an integer. The returned value is stored
in the variable result.
Methods can return values of different types.
public class Main {
static int getAge() {
return 20;
}
static double getPrice() {
return 99.50;
}
static String getName() {
return "Amit";
}
static boolean isPassed() {
return true;
}
public static void main(String[] args) {
System.out.println(getAge());
System.out.println(getPrice());
System.out.println(getName());
System.out.println(isPassed());
}
}
Output:
20
99.5
Amit
true
The returned value must be compatible with the method's declared return type.
| Void Method | Return Method |
|---|---|
| Does not return a value. | Returns a value of a specified type. |
Uses the void keyword. |
Uses a type such as int or String. |
static void greet() |
static int add() |
Void method:
static void greet() {
System.out.println("Hello!");
}
Return method:
static int square(int number) {
return number * number;
}
Use a void method when you need an action performed. Use a return method when you need a result to use elsewhere.
Calculate the area of a rectangle using a method.
public class Main {
static int calculateArea(int length, int width) {
return length * width;
}
public static void main(String[] args) {
int area = calculateArea(10, 5);
System.out.println("Area = " + area);
}
}
Output:
Area = 50
The method receives the length and width, multiplies them, and returns the area.
A static method belongs to the class. An instance method is called on an object of that class.
public class Main {
static void staticMethod() {
System.out.println("Static method");
}
void instanceMethod() {
System.out.println("Instance method");
}
public static void main(String[] args) {
staticMethod();
Main obj = new Main();
obj.instanceMethod();
}
}
Output:
Static method
Instance method
A static method can be called directly from the same class. An instance method normally requires an object to call it.
Method overloading means defining multiple methods with the same name but different parameter lists.
public class Main {
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(add(10, 20, 30));
System.out.println(add(2.5, 3.5));
}
}
Output:
30
60
6.0
Overloaded methods must differ in their parameter lists. Changing only the return type is not enough.
Write Java programs to complete these assignments.
1. Which keyword indicates that a method does not return a value?
2. Which statement sends a value back from a method?
3. What is method overloading?