Calling methods in Java is one of the fundamental building blocks of the language. Whether you’re a beginner or an experienced programmer brushing up on the basics, understanding the different ways you can call a method is key to writing efficient and clean Java code.
This tutorial will walk you through the multiple ways to call a method in Java, from basic calls to more advanced use cases involving reflection and method references.
What Is a Method in Java?
In Java, a method is a block of code that performs a specific task. Methods are defined within a class and can be called or invoked to execute the code they contain.
A typical method definition looks like this:
public int add(int a, int b) { return a + b; }
Now, let’s explore the different ways to invoke such methods.
1. Calling a Method Using an Object (Instance Method)
This is the most common way to call a method in Java. You create an object of the class and use that object to invoke the method.
Example:
public class Calculator { public int add(int a, int b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); int result = calc.add(5, 3); System.out.println("Result: " + result); } }
Key Point:
- This is used for non-static methods.
- Requires object instantiation.
2. Calling a Static Method
Suppose a method is declared static, belonging to the class rather than an object. So, you can call it directly using the class name.
Example:
public class MathUtil { public static int square(int number) { return number * number; } } public class Main { public static void main(String[] args) { int result = MathUtil.square(6); System.out.println("Square: " + result); } }
Key Point:
- No need to create an object.
- Best for utility methods.
3. Calling a Method from Another Method
You can also call one method from within another inside the same class. This can help organize code and reduce redundancy.
Example:
public class Printer { public void greet() { System.out.println("Hello!"); showDate(); } public void showDate() { System.out.println("Today is Monday."); } }
Key Point:
- Promotes code reuse.
- Helps break down complex operations into smaller parts.
4. Calling Methods via this
Keyword
When you want to refer to the current class instance, you can use this
to call non-static methods from within the same class.
Example:
public class Counter { int count = 0; public void increment() { this.count++; this.showCount(); } public void showCount() { System.out.println("Count is: " + count); } }
Key Point:
- Helpful when dealing with constructor chaining or avoiding naming conflicts.
5. Using Method References (Java 8+)
Method references allow you to call methods in a very concise and readable way. This is often used with functional interfaces and streams.
Example:
import java.util.Arrays; public class MethodRefExample { public static void print(String s) { System.out.println(s); } public static void main(String[] args) { String[] names = {"Alice", "Bob", "Charlie"}; Arrays.stream(names).forEach(MethodRefExample::print); } }
Key Point:
- Clean syntax.
- Works with static and instance methods.
- Commonly used in functional programming.
6. Calling Methods via Reflection
Reflection allows you to inspect and call methods dynamically at runtime. It’s powerful but should be used carefully due to performance and security implications.
Example:
import java.lang.reflect.Method; public class ReflectExample { public void sayHi() { System.out.println("Hi from Reflection!"); } public static void main(String[] args) throws Exception { ReflectExample obj = new ReflectExample(); Method method = ReflectExample.class.getMethod("sayHi"); method.invoke(obj); } }
Key Point:
- Used in frameworks like Spring, Hibernate, etc.
- Useful for dynamic behavior and plugin systems.
7. Calling a Method Using Lambda Expressions (Java 8+)
Lambda expressions allow you to pass methods as arguments, which is beneficial when working with collections or asynchronous code.
Example:
import java.util.function.Consumer; public class LambdaExample { public static void main(String[] args) { Consumer<String> printer = message -> System.out.println(message); printer.accept("Lambda says hello!"); } }
Key Point:
- Encourages functional programming.
- Useful in concise and inline method definitions.
8. Recursive Method Call
A method that calls itself is recursive. This powerful technique is used in algorithms like factorial, tree traversal, etc.
Example:
public class RecursionDemo { public int factorial(int n) { if (n == 1) return 1; return n * factorial(n - 1); } }
Key Point:
- A base condition must be used to avoid an infinite loop.
- Elegant for solving problems with repetitive patterns.
Summary Table

Finally
Calling methods in Java can be as straightforward or dynamic as your use case demands. Java offers multiple paths for flexibility and clean code structure, whether you’re invoking a basic instance method, leveraging the power of functional programming with method references, or going full throttle with reflection.