Menu

Java Methods: How to Define and Call Methods

What a Java method is, how to declare and call one, return values vs void, the static main method, and how methods keep code organized and reusable.

This page includes runnable editors - edit, run, and see output instantly.

Why Methods Exist

A method is a named block of code you can run whenever you call its name. Instead of writing the same lines over and over, you write them once inside a method and call it as many times as you need. That keeps programs shorter, easier to read, and far easier to fix - change the logic in one place and every caller gets the update.

You have already been calling methods: System.out.println(...) is a method call, and so is list.size(). Now you will write your own.

Declaring and Calling a Method

A method declaration has four parts: a return type, a name, a pair of parentheses (for parameters), and a body in braces. Here is the smallest useful example - a method that does some work and returns nothing:

Read the pieces of static void printGreeting():

  • static - lets main call it directly without creating an object (more on this below).
  • void - this method returns no value.
  • printGreeting - the name you use to call it.
  • () - empty parentheses mean it takes no inputs.

You call a method by writing its name followed by parentheses. The two calls in main run the body twice, so this prints the greeting twice.

Returning a Value

Most methods compute something and hand it back with return. The return type before the name says what kind of value comes out - here, int:

square(5) evaluates to 25, which you can store in a variable, print, or use in a larger expression - a method call stands in for the value it returns. The moment return runs, the method ends and control jumps back to the caller. Any code after a return on the same path never executes.

The return type and the returned value must match: the data type you declare is the kind of value the method promises to hand back. Declaring int and trying to return "text"; is a compile error - the compiler enforces the contract.

void vs a Return Type

The split is simple: use a real return type when the caller needs a value back, and void when the method just does something (prints, saves, updates state).

void logMessage(String msg)   // does work, hands nothing back
int  add(int a, int b)        // computes a value the caller uses

A common beginner mistake is trying to use the "result" of a void method:

int x = printGreeting();   // compile error: printGreeting() returns void

There is no value to assign, so this will not compile. If you find yourself wanting the result of a void method, that is a sign it should return something instead.

Methods That Branch and Return Early

You can return from more than one spot. Returning early - as soon as you know the answer - often reads more clearly than nesting everything in else:

Because each return exits immediately, the first matching condition wins. Just make sure every path through a value-returning method ends in a return - if some branch can fall through without returning, the compiler complains with "missing return statement". The final return "F"; here covers the fallthrough case.

What static Means Here

You may have noticed every method above is static. A static method belongs to the class itself, not to any object, so main (which is also static) can call it directly by name. Without static, the method belongs to an instance and you would need an object to call it on.

For now, while you are writing small programs whose logic lives alongside main, mark your helper methods static so they can be called the same way. Once you start building your own classes and objects, you will write non-static (instance) methods too - that comes with the classes chapter.

Local Variables Stay Inside the Method

Variables declared inside a method exist only there. They are created when the method runs and gone when it returns - the caller never sees them:

total and i live entirely inside sumTo. The only thing that escapes the method is the value you return. This isolation is a feature: each method is a self-contained unit you can reason about without worrying that its temporary variables leak out or collide with names elsewhere.

Next: Method Parameters

Every method here either took no input or a single value. The real power comes from passing data in - and Java has specific rules for how arguments are passed, what happens with objects versus primitives, and how to give a parameter a default-like fallback. That is the next page.

Frequently Asked Questions

What is a method in Java?

A method is a named block of code that you can run on demand by calling its name. It groups a piece of work in one place so you can reuse it instead of repeating the same lines. A method can take inputs (parameters) and hand back a result (a return value), or do its work and return nothing (void).

How do you create and call a method in Java?

Declare it with a return type, a name, and parentheses: static int square(int n) { return n * n; }. Then call it by name with arguments in parentheses: int result = square(5);. A method that returns nothing uses void and is called as a standalone statement, like printGreeting();.

What does void mean in a Java method?

void means the method returns no value - it does its work (like printing) and hands nothing back to the caller. You cannot assign a void method's result to a variable. If a method should produce a value the caller uses, give it a real return type like int or String and use return instead.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED