Method and Types of Methods

Java 9 min min read Updated: Mar 31, 2026 Beginner
Method and Types of Methods
Beginner Topic 10 of 25

Method and Types of Methods

In Java, a method is a block of code that performs a specific task. Methods are used to organize code, improve reusability, and reduce duplication. Instead of writing the same logic multiple times, a method allows you to define it once and reuse it wherever required.

Methods are one of the most important concepts in Java programming and are heavily used in both small programs and large enterprise applications.

Key Concept: A method is a reusable block of code that performs a specific task and can be called multiple times during program execution.

What is a Method?

A method is a group of statements that perform a particular function. It is defined inside a class and can be called using its name.

Example:

java class Demo { void greet() { System.out.println("Hello, welcome to Java!"); } }

Here, greet() is a method.

Why Methods are Important

  • Reduce code duplication
  • Improve readability and maintainability
  • Allow modular programming
  • Make debugging easier
  • Encourage code reuse

Syntax of a Method

java returnType methodName(parameters) { // method body }

Example

java int add(int a, int b) { return a + b; }

Explanation

  • int → return type
  • add → method name
  • (int a, int b) → parameters
  • return a + b; → return statement

Parts of a Method

  • Return Type: type of value returned (e.g., int, void)
  • Method Name: identifier used to call the method
  • Parameters: inputs to the method
  • Method Body: actual logic
  • Return Statement: sends result back to caller

Method Without Return Value (void)

A method with void return type does not return any value.

java void display() { System.out.println("This is a void method"); }

Method With Return Value

A method with a return type returns a value using the return statement.

java int square(int num) { return num * num; }

Calling a Method

A method is executed when it is called.

java public class Main { public static void main(String[] args) { Demo d = new Demo(); d.greet(); } }

Types of Methods in Java

Methods can be classified based on parameters and return values:

  • No parameters, no return value
  • Parameters, no return value
  • No parameters, return value
  • Parameters and return value

1. No Parameters, No Return Value

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

2. Parameters, No Return Value

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

3. No Parameters, Return Value

java int getNumber() { return 100; }

4. Parameters and Return Value

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

Method Overloading

Method overloading means having multiple methods with the same name but different parameter lists.

java class MathUtil { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }

This improves flexibility and readability.

Static Methods

Static methods belong to the class and can be called without creating an object.

java class Demo { static void show() { System.out.println("Static method"); } }
java Demo.show();

Instance Methods

Instance methods belong to objects and require object creation.

java class Demo { void display() { System.out.println("Instance method"); } }

Method with Arguments Example

java 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 Call by Value

Java uses call by value, meaning a copy of the value is passed to the method.

java class Test { void change(int x) { x = 50; } public static void main(String[] args) { int a = 10; Test t = new Test(); t.change(a); System.out.println(a); } }

Output remains 10 because original value is not changed.

Recursive Methods

A method that calls itself is called a recursive method.

java int factorial(int n) { if (n == 1) return 1; return n * factorial(n - 1); }

Varargs Method

Varargs allow passing variable number of arguments.

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

Method Execution Flow

When a method is called:

  • Control moves to method
  • Method executes statements
  • Returns result (if any)
  • Control goes back to caller

Common Mistakes

  • Forgetting return statement in non-void methods
  • Wrong parameter types
  • Confusing static and instance methods
  • Infinite recursion without base condition

Best Practices

  • Use meaningful method names
  • Keep methods small and focused
  • Avoid too many parameters
  • Use return values instead of printing inside methods
  • Follow naming conventions (camelCase)

Interview-Oriented Points

  • A method is a reusable block of code
  • Java supports method overloading
  • Java uses call by value
  • Static methods belong to class
  • Instance methods belong to object
  • Recursive methods call themselves

Conclusion

Methods are essential for writing clean, modular, and reusable code in Java. Understanding different types of methods and how they work helps in building scalable applications.

From simple utility methods to complex business logic, methods form the backbone of Java programming.

Quick Summary: Methods are reusable blocks of code that improve modularity, reduce duplication, and help organize program logic efficiently.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators