What a method is and why you need one
A method in Java is a block of code that does one specific job. Instead of writing the same lines over and over, you write them once in a method, give that method a name, and then call it by name whenever you need that job done. Methods let you organize your code into smaller, reusable pieces.
Think of a method like a recipe. You write down the steps once, name it "Make Toast", and then whenever someone asks you to make toast, you follow that recipe instead of explaining every step from scratch. In Java, you write a method once and call it as many times as you need.
Every Java program uses methods. When you write System.out.println("Hello"), you are calling a method that Java already wrote for you. When you write your own methods, you are building the tools your program will use.
Key Takeaways
- A method starts with a return type (what it gives back), a name, and parentheses that hold information the method needs to work.
- The simplest method to start with returns nothing and takes no information — it just does a job when you call it.
- You call a method by typing its name followed by parentheses, and the code inside the method runs when ready.
- Methods save you from writing the same code twice and make your program easier to read and change later.
- Every method lives inside a class, and you write the method body between curly braces.
The basic shape of a method
Every method in Java follows the same pattern. Here is the simplest one:
public static void sayHello() { System.out.println("Hello, World!"); }
Break this down piece by piece. public means any other code can call this method. static means you can call it without creating an object first — you just use the method name directly. void means the method does not give anything back; it just does a job. sayHello is the name you chose for this method. The empty parentheses () mean this method does not need any information to work. The curly braces {} hold the code that runs when you call the method.
Inside the braces, you write the actual work. In this case, it prints "Hello, World!" to the screen. When you call sayHello() anywhere in your program, those lines run when ready.
Methods that take information
Most methods need information to work with. If you want a method that greets someone by name, you need to tell it what name to use. Information a method needs is called a parameter.
public static void greet(String name) { System.out.println("Hello, " + name); }
Inside the parentheses, you write the type of information (in this case String, which means text) and a name for that information (name). Now when you call this method, you have to give it a name: greet("Alice") or greet("Bob"). The method uses whatever name you pass in.
A method can take more than one piece of information. Separate them with commas:
public static void add(int a, int b) { int result = a + b; System.out.println(result); }
This method takes two whole numbers, adds them, and prints the result. You would call it like add(5, 3).
Methods that give something back
Some methods do not just print a result — they give a value back to the code that called them. This is called a return value. Instead of void, you write the type of thing the method gives back.
public static int multiply(int a, int b) { int result = a * b; return result; }
This method takes two numbers, multiplies them, and gives the result back. The int at the start tells Java that this method returns a whole number. The return keyword sends that value back to whoever called the method.
When you call a method that returns something, you usually store the answer in a variable:
int answer = multiply(4, 5); System.out.println(answer);
Now answer holds 20, and you can use it in the rest of your program.
Where methods live in your code
Every method must live inside a class. When you create a new Java file, you are creating a class. Methods go inside the class but outside any other method.
public class Calculator { public static void sayHello() { System.out.println("Hello"); } public static int add(int a, int b) { return a + b; } }
Notice that each method is indented inside the class. The class has its own opening and closing curly brace, and each method has its own. This structure matters — Java will not run your code if the braces do not match up.
Common mistakes when writing methods
The most common mistake is forgetting the parentheses when you call a method. sayHello does nothing — Java thinks you are just mentioning the method's name. sayHello() actually runs it.
Another mistake is forgetting the return keyword when your method is supposed to give something back. If you write public static int add(int a, int b), Java expects you to use return somewhere in that method. If you do not, Java will show an error.
A third mistake is mismatching the number or type of parameters. If a method expects two numbers but you call it with one, or if you pass text when it expects a number, Java will stop and show an error. The types have to match exactly.
Frequently Asked Questions
What is the difference between void and other return types?
void means the method does not give anything back — it just does a job. If you write public static int or public static String, the method must use return to send a value back. The type you write (int, String, double) must match what you return.
Can a method call another method?
Yes. Inside one method, you can call any other method in the same class. You can also call methods from other classes if they are public. This is how larger programs work — methods call other methods to break the work into smaller pieces.
What happens if I do not give a method any parameters?
That is fine. The method just does the same thing every time you call it. Use empty parentheses () and the method does not need any information from you.
Can I have two methods with the same name?
Only if they have different parameters. You can have add(int a, int b) and add(double a, double b) in the same class. Java tells them apart by looking at the types of information they take. This is called overloading.
Why do I need the static keyword?
static means you can call the method without creating an object first. For now, use static on all your methods. Later, when you learn about objects, you will write methods without static that belong to specific objects instead.