A method is a block of code that performs a specific task and can be reused throughout your program

In Java, a method is a set of instructions grouped together under a single name. Instead of writing the same code over and over, you write it once as a method, then call that method by name whenever you need it. Methods make your code shorter, easier to read, and simpler to fix when something breaks.

Every method has a structure: it declares what type of data it returns (or returns nothing), gives itself a name, lists what information it needs to work with, and contains the actual code inside curly braces. You define the method once, then use it as many times as you want by typing its name followed by parentheses.

Key Takeaways

  • A method starts with an access level (usually public or private), a return type (like int, String, or void), a name, and parentheses that hold parameters.
  • The method body — the actual code — goes inside curly braces and runs only when you call the method by name.
  • A method that returns a value must use the return keyword to send data back to wherever the method was called.
  • Methods can take zero, one, or multiple parameters, and each parameter must have a declared type.
  • You call a method by writing its name, parentheses, and any required data inside — for example, calculateTotal(50, 25).

The basic structure of a method

Every Java method follows the same pattern. Start with an access modifier — usually public (anyone can use it) or private (only code inside this class can use it). Next comes the return type, which tells Java what kind of data the method sends back. If the method does not send anything back, use void. Then write the method name, followed by parentheses that hold the parameters — the data the method needs to do its job.

Here is a simple example:

public void greetUser(String name) {   System.out.println("Hello, " + name); }

This method is named greetUser, takes one parameter called name (which must be text), and returns nothing (void). The code inside the curly braces runs when you call the method.

To call this method, you write:

greetUser("Alice");

Java runs the code inside the method, prints "Hello, Alice" to the screen, and then continues with the rest of your program.

Methods that return a value

Many methods calculate something and send the result back to the code that called them. Instead of void, you specify the type of data being returned — int for whole numbers, double for decimals, String for text, and so on. Inside the method, use the return keyword followed by the value you want to send back.

Here is a method that adds two numbers and returns the result:

public int addNumbers(int a, int b) {   int sum = a + b;   return sum; }

This method takes two integers as parameters, adds them together, and returns the result as an integer. When you call it, you can store the result in a variable:

int result = addNumbers(10, 20); System.out.println(result);

The variable result now holds 30. The return statement stops the method immediately — any code written after return will not run.

Methods with multiple parameters

A method can take as many parameters as you need. Each parameter must have a declared type, and you separate them with commas. When you call the method, you must provide values in the same order and type.

Here is a method that calculates the area of a rectangle:

public double calculateArea(double length, double width) {   double area = length * width;   return area; }

To use it, you pass two decimal numbers in the correct order:

double roomArea = calculateArea(5.5, 4.0);

If you pass the values in the wrong order or use the wrong type, Java will not compile your program. This type checking prevents mistakes before your code even runs.

Methods with no parameters

Not every method needs information from outside. Some methods do the same thing every time they run. In that case, leave the parentheses empty.

Here is a method that displays the current time:

public void displayTime() {   System.out.println("Current time: " + System.currentTimeMillis()); }

Call it without putting anything inside the parentheses:

displayTime();

Even though there are no parameters, you still need the parentheses when you call the method. They tell Java that you are calling a method, not referring to a variable.

Public versus private methods

The access modifier controls who can use your method. A public method can be called from anywhere in your program — from other classes, from other files, from anywhere. A private method can only be called from inside the same class where it is defined.

Use public for methods that are part of your class's main job — the methods other parts of your program need to use. Use private for helper methods that do small tasks only your class needs. This keeps your code organized and prevents other parts of your program from accidentally using methods they should not.

For example, a class that handles user login might have a public method called login() that anyone can call, and a private method called validatePassword() that only the login method uses internally.

Common mistakes when writing methods

One frequent error is forgetting the return statement in a method that should return a value. If your method declares a return type like int or String, Java requires that every path through the code reaches a return statement. If you forget it, your program will not compile.

Another mistake is mismatching the parameter types when you call a method. If a method expects an integer but you pass a string, Java will reject it. Similarly, if a method returns an integer but you try to store the result in a string variable, Java will complain. Pay attention to types — they are your safety net.

A third common issue is giving methods names that do not describe what they do. A method called process() tells you nothing. A method called calculateMonthlyPayment() tells you exactly what it does. Good method names make your code readable and save time when you come back to it weeks later.

Frequently Asked Questions

Can a method call another method?

Yes. Inside one method, you can call any other method that is accessible to you. A public method can call other public methods, and any method can call private methods within the same class. This is how you build complex behavior from simpler pieces.

What happens if a method has no return statement?

If the return type is void, the method does not need a return statement — it just runs and stops. If the return type is anything else (like int or String), Java will not let your code compile without a return statement that sends back the correct type of data.

Can I have two methods with the same name?

Yes, if they have different parameter types or different numbers of parameters. This is called method overloading. For example, you could have addNumbers(int a, int b) and addNumbers(double a, double b). Java knows which one to use based on what data you pass in.

Do I have to use all the parameters a method receives?

Technically yes, but it is bad practice to declare a parameter you do not use. If you find yourself not using a parameter, remove it from the method definition. Unused parameters confuse anyone reading your code and suggest the method might not be doing what it should.

What is the difference between a method and a function?

In Java, the term method refers to a function that belongs to a class. Other languages use function for standalone code blocks. In Java, everything is part of a class, so we call them methods. The concepts are the same — both are reusable blocks of code.