Java Methods

Last Updated : 7 Oct, 2025

Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.

Java program to demonstrate how to create and use a method.

Java
public class Geeks
{
    // An example method
    public void printMessage() {
        System.out.println("Hello, Geeks!");
    }
    
    public static void main(String[] args) {
        
        // Create an instance of the class
        // containing the method
        Geeks obj = new Geeks();
        
        // Calling the method
        obj.printMessage(); 
    }
}

Output
Hello, Geeks!

Explanation:

  • Here, first we create a method which prints Hello, Geeks!
  • printMessage() is a simple method that prints a message.
  • It has no parameters and does not return anything.

Syntax of a Method

Method Body in Java

Key Components of a Method Declaration

Method consists of a modifier (Define access level), return type (what value returned or void), name (Define the name of method follows camelCase), parameters (optional inputs), and a body (Write your logic here).

Why Use Methods?

Breaking code into separate methods helps improve readability, reusability, and maintainability