Open links in new tab
  1. In Java, a method is a block of code designed to perform a specific task. Methods help in code reusability, modularity, and readability by allowing you to write logic once and use it multiple times. Every method in Java must be declared inside a class and can be either predefined (from Java libraries) or user-defined.

    Basic Syntax:

    modifier static returnType methodName(parameters) {
    // method body
    }
    Copied!
    • modifier: Access level (e.g., public, private).

    • static: Allows calling without creating an object.

    • returnType: Data type of the returned value or void if none.

    • methodName: Follows camelCase naming.

    • parameters: Optional inputs.

    Example: Creating and Calling a Method

    public class Main {
    static void greet() {
    System.out.println("Hello, World!");
    }
    public static void main(String[] args) {
    greet(); // method call
    }
    }
    Copied!

    Output:

    Hello, World!
    Copied!

    Here, greet() is a static method with no parameters and no return value.

    Instance vs Static Methods:

    class Calculator {
    int add(int a, int b) { // instance method
    return a + b;
    }
    static int square(int x) { // static method
    return x * x;
    }
    }
    public class Test {
    public static void main(String[] args) {
    Calculator calc = new Calculator();
    System.out.println(calc.add(5, 3)); // instance call
    System.out.println(Calculator.square(4)); // static call
    }
    }
    Copied!
  1. Java Methods - GeeksforGeeks

    Mar 13, 2026 · Java is an object-oriented and stack-based programming language where methods play a key role in controlling the program's execution flow. When a …

  2. Defining Methods (The Java™ Tutorials > Learning the Java Language ...

    Learn how to declare methods in Java, including modifiers, return types, parameters, and exceptions. See examples of overloading methods with different signatures.

  3. Java Methods (With Examples) - Programiz

    Learn what a method is and how to declare, call, and use methods in Java. Find out the difference between user-defined and standard library methods, and see …

  4. Java - Methods - Online Tutorials Library

    Learn how to create, call, and overload methods in Java with or without return values and parameters. See the syntax, examples, and output of methods in this tutorial.

  5. Methods in Java: Syntax, Types and Simple Examples

    Jan 30, 2026 · Learn methods in Java with clear explanations, syntax, examples, and interview questions. Perfect for Java beginners.

  6. People also ask
    Loading
    Unable to load answer
  7. Methods in Java – Explained with Code Examples

    Feb 29, 2024 · Learn what Java methods are and how they work, including their syntax, types, and examples. Methods are essential for organizing Java projects, encouraging code reuse, and …

  8. Methods in Java - Tpoint Tech

    Feb 10, 2026 · Learn what is a method in Java, types of methods, method declaration, and how to call a method in Java. See examples of predefined and user-defined …

  9. Java Classes and Methods: A Comprehensive Guide

    Dec 22, 2025 · This blog provides a comprehensive overview of Java classes and methods. By following the concepts and practices outlined here, you will be well-on your way to becoming a proficient Java …

  10. Java Methods - Codecademy

    Oct 21, 2021 · Methods in Java are reusable blocks of code that perform a specific task. They help in breaking down large programs into smaller, reusable sections, …