How To Call Another Class Method In Java

6 min read

Calling another class method in Java is one of the most important skills you will learn as you move from writing simple programs to building real applications. In Java, almost everything is organized around classes, and most useful behavior is placed inside methods. That said, once you understand how to call another class method in Java, you can create cleaner code, reuse logic, and design programs that are easier to maintain. Whether you are working with static methods, instance methods, inheritance, or composition, Java gives you several clear and reliable ways to connect classes together.

You'll probably want to bookmark this section.

Introduction: Why Calling Another Class Method Matters

Java is an object-oriented programming language, which means programs are usually built from multiple classes that work together. Plus, a class may represent a user, a database connection, a calculator, a payment service, or any other logical unit. The methods inside those classes define what each object can do.

Here's one way to look at it: a UserService class might contain a method called registerUser(), while a NotificationService class might contain a method called sendEmail(). To make the application work, one class often needs to call a method from another class. This is why learning how to call another class method in Java is essential Surprisingly effective..

The good news is that Java makes this process straightforward once you understand the difference between static methods and instance methods, and once you know how objects are created and used.

Understanding Classes, Objects, and Methods

Before calling a method from another class, it helps to review three basic ideas:

  • A class is a blueprint. It defines fields and methods.
  • An object is a real instance created from that class.
  • A method is a block of code that performs a specific task.

For example:

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

Here, Calculator is a class, and add is a method. To use this method from another class, you usually need to create an object of Calculator first, unless the method is declared as static.

The Two Main Ways to Call Another Class Method

There are two primary situations you will encounter:

  1. Calling an instance method using an object of the class.
  2. Calling a static method using the class name.

Both are common, but they are used in different situations.

1. Calling an Instance Method Through an Object

An instance method belongs to an object, not to the class itself. To call it, you must first create an object.

Example:

public class Greeter {
    public void sayHello() {
        System.out.println("Hello!");
    }
}

Now, from another class:

public class Main {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        greeter.sayHello();
    }
}

In this example:

  • Greeter is the class that contains the method.
  • greeter is an object of that class.
  • sayHello() is called using the object.

We're talking about the most common way to call another class method in Java when the method depends on object data.

2. Calling a Static Method Through the Class Name

A static method belongs to the class itself rather than to any specific object. You can call it directly using the class name.

Example:

public class MathHelper {
    public static int square(int number) {
        return number * number;
    }
}

From another class:

public class Main {
    public static void main(String[] args) {
        int result = MathHelper.square(5);
        System.out.println(result);
    }
}

Here, no object is created. The method is called using MathHelper.square(5) Simple, but easy to overlook..

Static methods are useful for utility functions, constants, or operations that do not require object state.

Step-by-Step Example: Calling a Method From Another Class

Suppose you have two classes: Employee and PayrollCalculator.

The Employee class stores basic information:

public class Employee {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }
}

The PayrollCalculator class uses the employee data:

public class PayrollCalculator {
    public double calculateAnnualSalary(Employee employee) {
        return employee.getSalary() * 12;
    }
}

Now, in a Main class:

public class Main {
    public static void main(String[] args) {
        Employee employee = new Employee("Alice", 3500);
        PayrollCalculator calculator = new PayrollCalculator();

        double annualSalary = calculator.calculateAnnualSalary(employee);
        System.out.

This example shows a very common pattern:

1. Create an object of the class you need.
2. Pass required data into the method.
3. Call the method using the object.
4. Use the returned value.

This is a practical answer to **how to call another class method in Java** in real applications.

## Calling Methods Between Classes in the Same Project

When both classes are in the same project, calling a method is usually simple. You just need to make sure the method is accessible.

Take this: if `ServiceA` needs to call `ServiceB`:

```java
public class ServiceB {
    public String getData() {
        return "Sample data";
    }
}
public class ServiceA {
    public void process() {
        ServiceB serviceB = new ServiceB();
        String data = serviceB.getData();
        System.out.println(data);
    }
}

If the classes are in different packages, you must import the class you want to use. For example:

import com.example.ServiceB;

On the flip side, since the instruction is to avoid external links and keep the explanation self-contained, the key

Continuing from “Calling Methods Between Classes”

When you move beyond simple instance‑based invocations, the same rules apply—you still need the right access level and visibility.
Because of that, if both classes reside in the same package, a plain ServiceA. So process() can reach ServiceB. getData() without any extra work. When the two modules live in different packages, you must either declare the reference to ServiceB inside ServiceA as public, or explicitly import the class with a leading dot (Service.BgetData()), depending on your coding style Which is the point..

// In ServiceA (same directory/package)
public class ServiceA {
    public void process() {
        ServiceB serviceB = new ServiceB();   // works because it’s in the same package
        String data = serviceB.getData();
        System.out.println(data);
    }
}

If you place the classes in distinct folders (e.example.g.On the flip side, serviceAandcom. , `com.example Practical, not theoretical..

import com.example.ServiceB;

and then refer to the member unqualified Not complicated — just consistent..

Now let’s turn attention back to static methods, which were hinted at at the very start of this article. But because it lives in the class definition, its name and signature are resolved by looking up the class named, not an object. A method declared static belongs to the class itself rather than to any particular instance. This means you never need to instantiate the helper class before invoking it No workaround needed..

Most guides skip this. Don't Worth keeping that in mind..

Calling a Static Method from Another Class

Suppose you have placed a utility inside a dedicated package:

// Utility.java (in com.example.MathUtils)
public class MathUtils {
    public static int square(int n) {
        return n * n;
    }
}

In the Main class you can call it directly via the fully qualified name:

public class Main {
    public static void main(String[] args) {
        int result = MathUtils.square(5);   // no object needed, just the class name
        System.out.println(result);         // prints 25
    }
}

Because the method is static, it can be reached from anywhere that knows the class name—

Dropping Now

Just Released

Handpicked

You Might Want to Read

Thank you for reading about How To Call Another Class Method In Java. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home