How to Call a Method in Java?
Last Updated :
12 Jul, 2025
In Java, calling a method helps us to reuse code and helps everything be organized. Java methods are just a block of code that does a specific task and gives us the result back. In this article, we are going to learn how to call different types of methods in Java with simple examples.
What is a Method in Java?
Before moving forward, let us first know what a method is in Java.
- A method is a block of statements that performs a specific task.
- A method can take input, can return a value, and can also be used multiple times.
Types of Methods in Java:
In Java, there are two types of methods:
- User-defined methods: These are the ones that we create to solve a specific problem.
- Predefined methods: These are the ones provided by Java libraries.
Now, we are going to discuss how to call different types of methods in Java.
Calling Different Types of Methods in Java
1. Calling a User-Defined Method
User-defined methods are blocks of code written by the programmer. These methods improve code reusability and readability. To execute a user-defined method, we first create an object of the class (if the method is non-static) and then call the method using that objec. Below code example demonstrates how to create user defined method and how to call.
Example:
Java
// Java program to demonstrates how
// to write a user defined method
class Geeks {
// User-defined method
void hello() {
System.out.println("This is a user-defined method.");
}
public static void main(String[] args) {
// Create an object
Geeks obj = new Geeks();
// Call the method
obj.hello();
}
}
OutputThis is a user-defined method.
Note: User-Defined non-static methods can be called or accessed only with the help of an instance of the class.
2. Calling the Abstract Methods
Abstract methods are declared inside abstract classes without an implementation. These methods are overridden by subclasses, which provide the actual implementation. Since abstract methods have no body, they cannot be called directly; instead, they are invoked through an object of the subclass that implements them. Below code example demonstrates how to create Abstract method and how to call.
Example:
Java
// Java Program to call Abstract Methods
// Helper class acting
// as Abstract class
abstract class Geekshelp {
// Creating abstract method
abstract void check(String n);
}
// Main class extending to helper class
public class Geeks extends Geekshelp {
public static void main(String[] args) {
// Creating the instance of the class
Geeks ob = new Geeks();
// Accessing the abstract method
ob.check("Geeksforgeeks");
}
// Extends the abstract method
@Override void check(String n)
{
System.out.println(n);
}
}
Explanation: check() method is declared in the abstract class Geekshelp and implemented in the subclass Geeks. The method is called with the string "Geeksforgeeks" and prints it.
3. Calling the Predefined Methods
These methods are provided by Java’s standard library, such as hashCode() from the Object class.
Example:
Java
// Java Program to call Predefined Methods
public class Geeks {
public static void main(String[] args) {
// Creating object of the class in
// main() method
Geeks ob = new Geeks();
// Calling predefined method
System.out.println(ob.hashCode());
}
}
Explanation: The predefined hashCode() method is inherited from the Object class. The method returns a unique integer hash code for the Geeks object.
4. Calling the Static Methods
Static methods are belong to the class rather than any specific instance of the class. This means they can be called without creating an object. Below code example demonstrates how to create Static method and how to call.
Example:
Java
// Java Program to call Static Methods
import java.io.*;
class test {
// Static method
static void hello()
{
System.out.println("Hello");
}
}
class Geeks {
public static void main(String[] args) {
// calling the Method 1
// Accessing method
test.hello();
}
}
Explanation: we call a static method hello() from the test class without creating an instance of the class. The method prints "Hello" when invoked from the main method.
Related article: