Open In App

How to Call a Method in Java?

Last Updated : 23 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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();          
    }
}

Output
This 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);
    }
}

Output
Geeksforgeeks

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());
    }
}

Output
1510467688

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();
    }
}

Output
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.


Next Article
Article Tags :
Practice Tags :

Similar Reads