Skip to main content
Java Basics
CHAPTER 11 Beginner

Methods in Java

Updated: May 17, 2026
5 min read

# CHAPTER 11

Methods in Java

1. Introduction

Methods are reusable blocks of code that perform a specific task. Instead of writing the same calculation code 50 times, you write it once in a method and call it whenever needed. Methods are the foundation of clean, maintainable code.

2. Learning Objectives

  • Declare and call methods.
  • Use parameters and return values.
  • Understand method overloading.
  • Implement basic recursion.

3. Method Declaration

java
1234
accessModifier returnType methodName(parameters) {
    // method body
    return value; // if returnType is not void
}
java
123456789101112
public static int add(int a, int b) {
    return a + b;
}

public static void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

public static void main(String[] args) {
    int sum = add(5, 3);           // 8
    greet("Alice");                 // Hello, Alice!
}

4. Parameters and Arguments

  • Parameters: Variables defined in the method declaration.
  • Arguments: Actual values passed when calling the method.
java
123456
public static double calculateArea(double length, double width) {
    return length * width;
}

// Calling with arguments
double area = calculateArea(10.5, 5.0);

5. Return Types

  • void — returns nothing
  • int, double, String, etc. — returns a value of that type
java
123
public static boolean isEven(int num) {
    return num % 2 == 0;
}

6. Method Overloading

Same method name, different parameter lists.
java
12345678
public static int add(int a, int b) { return a + b; }
public static double add(double a, double b) { return a + b; }
public static int add(int a, int b, int c) { return a + b + c; }

// Java picks the right version based on arguments
add(5, 3);        // calls int version
add(2.5, 3.7);    // calls double version
add(1, 2, 3);     // calls 3-parameter version

7. Variable Arguments (Varargs)

java
12345678
public static int sum(int... numbers) {
    int total = 0;
    for (int n : numbers) total += n;
    return total;
}

sum(1, 2);           // 3
sum(1, 2, 3, 4, 5);  // 15

8. Recursion

A method calling itself.
java
123456
public static int factorial(int n) {
    if (n <= 1) return 1;          // Base case
    return n * factorial(n - 1);    // Recursive case
}

System.out.println(factorial(5)); // 120 (5*4*3*2*1)

9. Common Mistakes

  • Missing return statement: Non-void methods must always return a value.
  • Infinite recursion: Forgetting the base case causes StackOverflowError.
  • Confusing overloading with overriding: Overloading is same class; overriding is inheritance.

10. MCQ Quiz with Answers

Question 1

A method with no return value uses:

Question 2

What is method overloading?

Question 3

What does return do?

Question 4

What is recursion?

Question 5

Without a base case, recursion causes:

Question 6

int... nums is called:

Question 7

Can two methods have the same name in Java?

Question 8

What is static in a method?

Question 9

What is 5! (factorial)?

Question 10

Parameters are defined in the method:

11. Interview Questions

  • Q: What is the difference between method overloading and overriding?
  • Q: Can you overload the main() method? (Yes, but JVM always calls public static void main(String[]))
  • Q: What is the call stack and how does recursion use it?

12. Summary

Methods are reusable code blocks with parameters and return types. Overloading allows multiple methods with the same name but different signatures. Recursion is a powerful technique but requires a base case to avoid stack overflow. Methods are the building blocks of Object-Oriented Programming.

13. Next Chapter Recommendation

In Chapter 12: Object-Oriented Programming Basics, we'll transition from procedural thinking to OOP — the paradigm that defines Java.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·