Calling a method in Java runs a named block of code, so you can perform an action or get a result without rewriting the same logic. First, make sure the method is declared in a class and that you can access it; then use the right syntax for its parameters and whether it belongs to an object or the class itself.
What You’ll Need
- A Java development environment or a text editor and Java compiler.
- A Java class containing the method you want to call, or a place to write a small example.
- A basic understanding of variables, classes, and Java statements.
Step-by-Step
- Find the method declaration. A method declaration tells you its name, whether it returns a value, what parameters it accepts, and whether it is static. For example:
static int add(int left, int right) { return left + right; }The method name isadd; it accepts twointvalues and returns anint. You must pass arguments compatible with its parameter types. - Check where the method is declared. If the method is in the same class as the code that calls it, you can usually refer to it by name. If it is in another class, you may need an instance of that class or the class name, depending on whether the method is static. Also check its access modifier: a method marked
privatecannot be called directly from unrelated classes. - Decide whether it is an instance method or a static method. An instance method belongs to an object. Create an object, then call the method through that variable. A static method belongs to the class itself, so call it through the class name. The declaration includes the keyword
staticwhen a method is static. Do not assume that a method is static just because it looks like a utility. - Create an object when the method is not static. Suppose a class declares
void greet(). You can create an instance and call the method like this:Greeter greeter = new Greeter();greeter.greet();
The expressionnew Greeter()constructs the object, andgreeterstores a reference to it. Use the class’s available constructor; if it requires arguments, supply them when creating the object. - Call a static method through its class. For the earlier
addexample, useint total = Calculator.add(4, 7);if the method is declared static in a class namedCalculator. This runs the method and stores its returned value intotal. If the call is in the same class, the class name may be omitted, but writing it can make the method’s location clearer. - Pass arguments in the declared order. The values inside the parentheses are called arguments. They must match the method’s parameter count and types, in the order declared. For example,
add(4, 7)passes the first value toleftand the second toright. A method declared asprintName(String name)needs a string argument, such asprintName("Mina"); calling it with an integer will not compile. - Handle the return value correctly. A method with a return type such as
int,String, orbooleanproduces a value. You can store it, use it in an expression, or pass it to another method. For example,System.out.println(Calculator.add(4, 7));callsaddand prints the result. A method declaredvoiddoes not return a value, so call it as a statement, such asgreeter.greet();. - Place the call inside executable code. A method call must appear in a valid location, such as the body of another method, a constructor, or an initializer. For a small runnable example, put the call inside
main:public class Demo {static int add(int left, int right) { return left + right; }public static void main(String[] args) {int total = add(4, 7);System.out.println(total);}}
Here,mainis static, andaddis also static, so it can be called directly frommain. - Compile and run the code. Save the source file with the same name as its public class, if it has one. Compile it using your chosen Java tools, then run the program. Read compiler messages before changing code: they often identify a missing parenthesis, a type mismatch, or a method that Java cannot find. If it compiles but the output is unexpected, check the method’s logic and the arguments passed to it.
Common Mistakes
Calling an instance method as if it were static. A call such as Greeter.greet() is not valid when greet is an instance method. Create a Greeter object and call greeter.greet() instead. Conversely, a static method can be called through its class without creating an object.
Leaving out parentheses. In Java, greet() is a method call; greet by itself is not the same thing. Include parentheses even when the method takes no arguments. A call with no parameters is written greet(), not greet.
Using the wrong argument count or types. If a method expects two parameters, provide two compatible arguments. Java checks the types at compile time in ordinary method calls, so a mismatch generally produces a compiler error rather than silently converting an incompatible value.
Ignoring a returned value. It is legal to call some value-returning methods as statements, but the result is then discarded. If the result matters, assign it to a variable or use it in an expression. Do not try to assign the result of a void method to a variable.
Confusing a declaration with a call. A declaration defines what the method does; a call asks Java to run it. A method declaration belongs inside a class, while a call belongs in executable code. Writing one does not automatically perform the other.
Forgetting access restrictions. A method may be unavailable from the code where you are trying to call it. Check whether it is private, package-private, protected, or public, and whether your code is in a permitted context. Do not make everything public as a quick fix; choose access based on how the class is intended to be used.
Tips
- Read the full method signature before calling it. The return type, name, parameter types, and access modifier all affect how a call should be written.
- Use descriptive variable names for objects and returned values. Names such as
greeterandtotalmake it easier to follow what each call does. - Prefer an object call for instance methods and a class-name call for static methods. Although Java may allow some static calls through an object reference, that style can be confusing.
- Break a complicated expression into smaller steps while learning. Store a method’s result in a variable first, then use that variable in the next operation.
- Remember that calling a method can change an object’s state or produce other effects, not just return a value. Check what the method body does before relying on it.
Troubleshooting
“Cannot find symbol” or a similar missing-method message: Check the spelling and capitalization of the method name; Java identifiers are case-sensitive. Confirm that the method is in scope, that you are using the correct class or object, and that the method signature actually exists. If the method is in another package, check the import and the class’s accessibility.
“Non-static method cannot be referenced from a static context”: The call is being made from static code, often main, but the target method requires an object. Create an instance and call the method through it. Make the method static only when that design makes sense and it does not need instance-specific state.
“Incompatible types” or “method cannot be applied”: Compare the arguments with the declared parameters. Check their order, count, and types. If a method returns a value, make sure the variable receiving it has a compatible type; if the method returns void, do not use it where a value is required.
The program compiles but nothing appears: A method call does not automatically display its return value. Print it, store it and inspect it, or check whether the method performs a visible action. Also verify that execution reaches the call and that the method’s conditions do not skip the expected behavior.
The wrong overloaded method runs: Java can provide several methods with the same name but different parameter lists. Check which overload matches the argument types you supplied. Use a clearly typed variable or an explicit conversion only when it is appropriate, rather than changing types blindly.
Frequently Asked Questions
How do I call a method in Java?
Write the method name followed by parentheses and any arguments, such as calculate(3). For a method in another class, use the class name if it is static or an object reference if it is an instance method.
What is the difference between a method call and a method declaration?
A declaration defines a method’s name, parameters, return type, and body. A call uses that method to run its code. The declaration alone does not run the method.
Can I call a method without creating an object?
Yes, if it is static and accessible from the calling code. Call it through its class name, or by its name when it is in the same class. An instance method normally requires an object.
How do I call a method with no parameters?
Use empty parentheses, as in reset(). If it is an instance method, call it through an object, such as settings.reset(); if static, use the class name as appropriate.
How do I use a method’s return value?
Assign it to a compatible variable or use the call in an expression. For example, boolean ready = isReady(); stores a returned boolean. A void method has no return value to store.
Can one Java method call another?
Yes. A method can call another method when the target is accessible and the call follows its static or instance requirements. The called method runs, then execution continues after that call returns.