← Back to Home

Methods Basics

A method in Java is a block of code designed to perform a specific task. Methods promote code reuse, modularity, readability, and maintainability, and they are a fundamental building block of any Java application. This topic is essential for interviews and for understanding OOP, execution flow, and program structure.

What Is a Method?

  • A named block of code
  • Executes when it is called (invoked)
  • May accept parameters
  • May return a value
int add(int a, int b) {
    return a + b;
}
          

Why Methods Are Important

  • Avoid code duplication
  • Improve readability
  • Simplify debugging and testing
  • Support modular and OOP design

Basic Syntax of a Method

accessModifier returnType methodName(parameterList) {
    // method body
}
          

Breakdown of Method Components

1. Access Modifier

Defines visibility.

  • public
  • protected
  • default (no keyword)
  • private
public int add(int a, int b)
          

2. Return Type

  • Specifies the type of value returned
  • Use void if no value is returned
void display() { }
          

3. Method Name

  • Should be a verb
  • Follows camelCase
calculateTotal()
          

4. Parameters (Optional)

  • Input values to the method
  • Defined with type and variable name
int add(int a, int b)
          

5. Method Body

  • Contains executable statements
  • Enclosed in {}

Example: Simple Method

public class Calculator {
    int add(int a, int b) {
        return a + b;
    }
    public static void main(String[] args) {
        Calculator c = new Calculator();
        int result = c.add(10, 20);
        System.out.println(result);
    }
}
          

Method Invocation (Calling a Method)

1. Calling Non-Static Method

Calculator c = new Calculator();
c.add(10, 20);
          

2. Calling Static Method

Math.max(10, 20);
          

Types of Methods in Java (Basics)

1. Predefined Methods

  • Provided by Java API
  • Example: println(), length(), max()

2. User-Defined Methods

  • Written by programmers
  • Customized logic

Methods Based on Return & Parameters

1. No Return, No Parameters

void greet() {
    System.out.println("Hello");
}
          

2. Return Value, No Parameters

int getNumber() {
    return 10;
}
          

3. No Return, With Parameters

void printSum(int a, int b) {
    System.out.println(a + b);
}
          

4. Return Value, With Parameters

int multiply(int a, int b) {
    return a * b;
}
          

return Statement

  • Sends value back to caller
  • Terminates method execution
return value;

return; // for void methods
          

Method Execution Flow

  1. Method is called
  2. Control transfers to method
  3. Parameters are initialized
  4. Method body executes
  5. Return value (if any) is sent back
  6. Control returns to caller

Method Overloading (Preview)

  • Same method name
  • Different parameter list
  • Compile-time polymorphism
int add(int a, int b) { }
int add(int a, int b, int c) { }
          

Common Beginner Mistakes

  • Forgetting return statement
  • Mismatch between return type and returned value
  • Incorrect method signature
  • Confusing static and non-static methods
  • Writing large methods instead of modular ones

Interview-Ready Answers

Short Answer

A method is a block of code that performs a specific task and is executed when called.

Detailed Answer

In Java, a method consists of an access modifier, return type, method name, parameters, and body. Methods enable code reuse, modular design, and better maintainability by encapsulating logic into reusable units.

Key Takeaway

Methods are the foundation of structured programming in Java. Writing small, clear, and reusable methods is essential for clean code and strong OOP design.