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:
drawCircle()– renders the circle outline.colorCircle()– fills the circle with a color.
Java offers two categories of methods:
- User‑defined Methods – crafted by developers to meet specific requirements.
- Standard Library Methods – pre‑built utilities provided by the Java Standard Library.
Declaring a Java Method
The simplest declaration looks like this:
returnType methodName() {
// method body
}
- returnType – the data type the method returns (e.g.,
int). Usevoidwhen nothing is returned. - methodName – the identifier used to invoke the method.
- method body – the code that executes when the method is called, enclosed in curly braces.
Example:
int addNumbers() {
// code
}
The complete syntax adds modifiers, static keyword, and parameters:
modifier static returnType methodName(parameter1, parameter2, ...) {
// method body
}
- modifier – access specifier (public, private, protected, etc.).
- static – allows the method to be invoked without an instance.
- parameters – values passed to the method.
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:
print()– fromjava.io.PrintStreamto output text.sqrt()– fromMathto compute square roots.
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
- Java Recursion: Understanding, Examples, and Trade‑Offs
- Mastering Method Overriding in Java
- Mastering Java Polymorphism: Concepts, Examples, and Best Practices
- Java Annotation Types: A Comprehensive Guide to Predefined, Custom, and Meta Annotations
- Mastering Java's Iterator Interface: Practical Guide with Code Example
- Mastering Java Methods: Create, Invoke, and Abstraction
- Java Method Overriding: Customizing Superclass Behavior
- Mastering Java 8: A Comprehensive Guide to Method References
- Java 8 Default Methods Explained: Enhancing Interfaces & Backward Compatibility
- C# Methods: Defining, Calling, and Using Functions