Methods in Java
Learn how Java methods organize reusable logic, accept input through parameters, and return results.
What is a Method?
A method is a named block of code that performs a specific task. Methods help make programs modular, reusable, readable, and easier to maintain.
static void greet() {
System.out.println("Welcome to CIIT Training Institute 🤓❤️ ....! ");
}
Method Syntax
accessModifier static returnType methodName(parameters) {
// method body
}
| Part | Purpose |
|---|---|
public |
Access modifier. |
static |
Allows the method to be called without creating an object. |
void |
Indicates that the method does not return a value. |
| methodName | Name used to call the method. |
| parameters | Input values supplied to the method. |
Calling a Method
public class Main {
static void greet() {
System.out.println("Welcome to CIIT Training Institute 🤓❤️ ....!");
}
public static void main(String[] args) {
greet();
}
}
The method is executed when its name is followed by
parentheses: greet();
Methods with Parameters
Parameters allow a method to receive data from the caller.
static void greet(String name) {
System.out.println("Hello " + name);
}
public static void main(String[] args) {
greet("Samadhan");
}
Multiple Parameters
static void displayUser(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
displayUser("Pradnya", 21);
}
Returning a Value
A method can return a result using the
return statement.
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println(result);
}
30
void Methods
A method declared with void does not return a
value.
static void printMessage() {
System.out.println("Welcome to CIIT Institute 🤓❤️....!");
}
Instance Methods
An instance method belongs to an object. To call it, an object of the class is normally created first.
class Student {
void study() {
System.out.println("Student is studying at CIIT Training Institute 👀👨🏫");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.study();
}
}
Static Methods
A static method belongs to the class rather than a particular object.
class Calculator {
static int square(int number) {
return number * number;
}
}
public class Main {
public static void main(String[] args) {
int result = Calculator.square(5);
System.out.println(result);
}
}
Arguments vs Parameters
| Term | Meaning |
|---|---|
| Parameter | Variable declared in the method definition. |
| Argument | Actual value passed when calling the method. |
static int add(int a, int b) {
return a + b;
}
add(10, 20);
Here a and b are parameters, while
10 and 20 are arguments.
Method Overloading
Java allows multiple methods with the same name as long as their parameter lists are different.
static int add(int a, int b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
Method overloading is covered in detail in the next lesson.
Recursion
Recursion occurs when a method calls itself. A recursive method must have a base condition to stop the recursion. Factorial is a common example used to understand recursion in Java.
Factorial Program Using Recursion
public class FactorialNumber {
static int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
int number = 5;
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
System.out.println("Learning Recursion successfully at CIIT Training Institute!");
}
}
Output
Factorial of 5 is: 120
Learning Recursion successfully at CIIT Training Institute!
Factorial Program: Line-by-Line Explanation
| Code | Explanation |
|---|---|
public class FactorialNumber
|
Defines a class named Main.
The Java program is written inside this class.
|
static int factorial(int n)
|
Defines a recursive method named
factorial. It accepts an integer
n and returns an integer result.
|
if (n == 0)
|
This is the base condition. When
n becomes 0,
the recursion stops.
|
return 1;
|
Returns 1 when n
is 0. Mathematically,
0! is equal to 1.
|
return n * factorial(n - 1);
|
This is the recursive call. The method calls
itself with n - 1 and multiplies
the returned value by n.
|
public static void main(String[] args)
|
This is the main method. Java starts executing
the program from the main() method.
|
int number = 5;
|
Creates an integer variable named
number and stores the value
5.
|
int result = factorial(number);
|
Calls the factorial() method with
number as the argument and stores
the returned result in the result
variable.
|
System.out.println(...)
|
Prints the calculated factorial result to
the console. For number = 5,
the result is 120.
|
System.out.println("Learning Recursion successfully at CIIT Training Institute!");
|
Prints a message confirming that the recursion example was completed successfully at CIIT Training Institute. |
How factorial(5) Works
The factorial method keeps calling itself with a smaller
value until it reaches the base condition
n == 0.
factorial(5)
↓
5 × factorial(4)
↓
5 × 4 × factorial(3)
↓
5 × 4 × 3 × factorial(2)
↓
5 × 4 × 3 × 2 × factorial(1)
↓
5 × 4 × 3 × 2 × 1 × factorial(0)
↓
5 × 4 × 3 × 2 × 1 × 1
↓
120
Factorial Formula
The factorial of a positive integer n is the
product of all positive integers from n down to
1.
For 5:
5! = 5 × 4 × 3 × 2 × 1 = 120
Java Pass-by-Value
Java is strictly pass-by-value. For primitive values, the value itself is copied. For object references, the reference value is copied.
static void change(int number) {
number = 100;
}
public static void main(String[] args) {
int value = 10;
change(value);
System.out.println(value);
}
10
Method Best Practices
- Give methods clear and meaningful names.
- Keep methods focused on one responsibility.
- Avoid unnecessarily long methods.
- Use parameters instead of duplicating similar logic.
- Choose an appropriate return type.
- Avoid excessive use of static methods when object-oriented design is more appropriate.
Summary
Methods are fundamental building blocks of Java programs. They allow logic to be organized into reusable units and can accept parameters and return values. Java supports static methods, instance methods, method overloading, and recursive methods.