Java Methods: Understanding Methods and Their Types
Introduction#
As our Java programs become bigger, writing all the code inside the main() method can make the program difficult to understand and maintain.
For example, imagine a calculator program that performs addition, subtraction, multiplication, and division.
Instead of writing all the logic together, we can divide the program into smaller blocks, where each block performs one specific task.
These blocks are called methods.
What Is a Method in Java?#
- A method is a block of code that performs a specific task.
- Instead of writing the same code repeatedly, we can place it inside a method and call it whenever required.
Simple example:
void greet() {
System.out.println("Hello!");
}
Why Do We Use Methods?#
- Code reusability
- Avoid code repetition
- Better organization
- Easier maintenance
- Easier debugging
- Makes large programs easier to understand
Basic Structure of a Method#
The basic structure of a method is:
returnType methodName(parameters) {
// method body
}
Let's understand each part.
Return Type#
The return type tells us what type of value the method will return.
For example:
int
means the method returns an integer.
If the method does not return any value, we use:
void
Example:
void greet() {
System.out.println("Hello!");
}
This method does not return a value.
Method Name#
The method name identifies the method.
For example:
greet
in:
void greet() {
System.out.println("Hello!");
}
The method name should describe what the method does.
Parameters#
Parameters are values that a method can receive as input.
For example:
void greet(String name) {
System.out.println("Hello " + name);
}
Here:
String name
is a parameter.
We will see how parameters work in more detail below.
Method Body#
The method body contains the actual code that performs the task.
For example:
void greet() {
System.out.println("Hello!");
}
The code inside { } is the method body.
Creating and Calling a Method#
Let's create a simple method:
class Main {
static void greet() {
System.out.println("Hello!");
}
public static void main(String[] args) {
greet();
}
}
Here we have created a method called greet():
static void greet() {
System.out.println("Hello!");
}
To execute the method, we call it:
greet();
Output
Hello!
An important point to remember is:
Defining a method does not execute it. We need to call the method to execute its code.
Methods With Parameters#
Sometimes a method needs some information to perform its task.
For example, if we want to greet a particular person, the method needs to know the person's name.
We can pass that value using a parameter.
static void greet(String name) {
System.out.println("Hello " + name);
}
Now we can call the method with different values:
greet("Ravi");
greet("Priya");
Output
Hello Ravi
Hello Priya
Here:
String name
is the parameter.
And:
"Ravi"
and:
"Priya"
are arguments.
Parameter vs Argument#
A parameter is the variable written in the method definition.
static void greet(String name) {
An argument is the actual value passed when calling the method.
greet("Ravi");
A simple way to remember:
Method definition
↓
Parameter
Method call
↓
Argument
Types of Methods in Java#
Methods can be classified in different ways.
One common classification is based on where the method comes from.
We have:
- Predefined methods
- User-defined methods
We can also classify methods based on parameters and return values.
Finally, methods can be static or instance methods.
Let's understand each one.
Predefined Methods#
Predefined methods are methods already provided by Java.
We can use these methods instead of writing the logic ourselves.
For example:
System.out.println("Hello");
and:
Math.max(10, 20);
Java provides many useful methods through its classes.
For example, Math.max() finds the larger value between two numbers.
int result = Math.max(10, 20);
System.out.println(result);
Output:
20
User-Defined Methods#
User-defined methods are methods created by the programmer according to the requirements of the program.
For example:
static void displayMessage() {
System.out.println("Welcome to Java");
}
This method is created by us.
We can call it whenever we need it:
displayMessage();
Methods Based on Parameters and Return Type#
Another useful way to understand methods is by looking at:
- Whether the method accepts parameters
- Whether the method returns a value
There are four common combinations.
No Parameter and No Return Value#
The method does not receive any value and does not return any value.
static void greet() {
System.out.println("Hello");
}
Calling:
greet();
Parameter but No Return Value#
The method receives a value but does not return anything.
static void greet(String name) {
System.out.println("Hello " + name);
}
Calling:
greet("Ravi");
No Parameter but Return Value#
The method does not receive any value but returns a value.
static int getNumber() {
return 10;
}
Calling:
int number = getNumber();
Parameter and Return Value#
The method receives values and also returns a value.
static int add(int a, int b) {
return a + b;
}
Calling:
int result = add(10, 20);
Quick Comparison
| Type | Parameters | Return Value |
|---|---|---|
| Type 1 | No | No |
| Type 2 | Yes | No |
| Type 3 | No | Yes |
| Type 4 | Yes | Yes |
These four combinations are important because most methods we create will fall into one of these categories.
Static Methods#
A static method belongs to the class rather than to a particular object.
Because of this, we can call a static method using the class name without creating an object.
For example:
class Calculator {
static int add(int a, int b) {
return a + b;
}
}
class Main {
public static void main(String[] args) {
int result = Calculator.add(10, 20);
System.out.println(result);
}
}
Output:
30
Here:
Calculator.add(10, 20);
calls the add() method using the class name.
Since add() is static, we do not need to create a Calculator object to call it.
Instance Methods#
An instance method belongs to an object.
Therefore, we need to create an object before calling an instance method.
For example:
class Student {
void display() {
System.out.println("Student details");
}
}
class Main {
public static void main(String[] args) {
Student s = new Student();
s.display();
}
}
Here:
Student s = new Student();
creates a Student object.
Then:
s.display();
calls the instance method using the object reference.
Static Method vs Instance Method#
| Feature | Static Method | Instance Method |
|---|---|---|
| Belongs to | Class | Object |
| Called using | Class name | Object reference |
| Object required | No | Yes |
| Example | Calculator.add() |
student.display() |
A simple way to remember:
Static Method
↓
Belongs to Class
↓
ClassName.method()
Instance Method
↓
Belongs to Object
↓
object.method()
Check Your Understanding#
1. Identify the Method Type
Consider:
static int add(int a, int b) {
return a + b;
}
Identify:
- Does it have parameters?
- Does it return a value?
- Is it a static or instance method?
- Is it predefined or user-defined?
2. Find the Problem
static int square(int n) {
n * n;
}
What is wrong with this method?
How should it be corrected?
3. Analyze the Method Call
static void greet(String name) {
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet("Ravi");
greet("Priya");
}
How many times is the method executed, and what will be printed?
4. Design a Method
Create a method that accepts two numbers and returns their larger value.
Decide:
- Method name
- Parameters
- Return type
- Method body