How to Call a Method in Java?
Last Updated :
23 May, 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
This is a very basic method that we define by ourselves with the help of a void or return type.
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 in abstract classes. They must be implemented in subclasses.
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: In the above example, the 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: In the above example, 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 belong to the class and can be called without creating an object.
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: In the above example, 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.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read