Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Methods: How to Define, Call, and Use Them Effectively

Java Methods

This guide explains how Java methods work, how to declare them, call them, and leverage both user‑defined and standard library methods with clear examples.

What is a Java Method?

A method is a self‑contained block of code that performs a single, well‑defined task. By breaking complex problems into smaller methods, you improve readability, maintainability, and reusability.

For instance, to draw and color a circle you might create:

This modular approach makes your code easier to understand and extend.

Java offers two categories of methods:

Declaring a Java Method

The simplest declaration looks like this:

returnType methodName() {
  // method body
}

Example:

int addNumbers() {
  // code
}

The complete syntax adds modifiers, static keyword, and parameters:

modifier static returnType methodName(parameter1, parameter2, ...) {
  // method body
}

Calling a Method

Once declared, invoke a method using its name followed by parentheses. Example:

addNumbers();

Example 1: User‑defined Method

class Main {
  // Instance method
  public int addNumbers(int a, int b) {
    int sum = a + b;
    return sum;
  }

  public static void main(String[] args) {
    int num1 = 25;
    int num2 = 15;
    Main obj = new Main();
    int result = obj.addNumbers(num1, num2);
    System.out.println("Sum is: " + result);
  }
}

Output

Sum is: 40

The method addNumbers() is invoked on an object because it is not static.

Return Types

Methods may return a value using the return statement. The returned value’s type must match the declared return type.

Example 2: Static method returning a value

class Main {
  public static int square(int num) {
    return num * num;
  }

  public static void main(String[] args) {
    int result = square(10);
    System.out.println("Squared value of 10 is: " + result);
  }
}

Output:

Squared value of 10 is: 100

If a method should not return anything, declare its return type as void:

public void display(int a) {
  System.out.println("Value is: " + a);
}

Parameters

Parameters are placeholders in a method’s signature that receive arguments during invocation. Java allows any number of parameters.

int addNumbers(int a, int b) { /* ... */ }
int addNumbers() { /* ... */ }

Example 3: Methods with different parameter counts

class Main {
  public void display1() {
    System.out.println("Method without parameter");
  }

  public void display2(int a) {
    System.out.println("Method with a single parameter: " + a);
  }

  public static void main(String[] args) {
    Main obj = new Main();
    obj.display1();
    obj.display2(24);
  }
}

Output

Method without parameter
Method with a single parameter: 24

Java’s strong typing ensures that the type of each argument matches its corresponding parameter.

Standard Library Methods

The Java Class Library (JCL) bundles many ready‑to‑use methods. Common examples include:

Example 4: Using a standard method

public class Main {
  public static void main(String[] args) {
    System.out.print("Square root of 4 is: " + Math.sqrt(4));
  }
}

Output:

Square root of 4 is: 2.0

Explore more library methods in the official Java API documentation.

Why Use Methods?

1. Code Reusability – Write once, use many times. For example:

public class Main {
  private static int getSquare(int x) {
    return x * x;
  }

  public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
      int result = getSquare(i);
      System.out.println("Square of " + i + " is: " + result);
    }
  }
}

Output:

Square of 1 is: 1
Square of 2 is: 4
Square of 3 is: 9
Square of 4 is: 16
Square of 5 is: 25

Using getSquare() avoids repetitive arithmetic code.

2. Readability & Debugging – Encapsulating logic in methods creates logical units that are easier to understand and test.


Java

  1. Java Recursion: Understanding, Examples, and Trade‑Offs
  2. Mastering Method Overriding in Java
  3. Mastering Java Polymorphism: Concepts, Examples, and Best Practices
  4. Java Annotation Types: A Comprehensive Guide to Predefined, Custom, and Meta Annotations
  5. Mastering Java's Iterator Interface: Practical Guide with Code Example
  6. Mastering Java Methods: Create, Invoke, and Abstraction
  7. Java Method Overriding: Customizing Superclass Behavior
  8. Mastering Java 8: A Comprehensive Guide to Method References
  9. Java 8 Default Methods Explained: Enhancing Interfaces & Backward Compatibility
  10. C# Methods: Defining, Calling, and Using Functions