3.2 Writing a void Method
How to Write a void Method in Java
Unit 3: Methods, Worlds, and Constructors · Lesson 3.2 · about 30 minutes · no coding experience needed
You have been calling methods since Unit 1. Now you write one. It is four pieces of punctuation and one rule about where it goes, and that rule is where everybody trips.
What you will be able to do
- Write a void method with a correct signature
- Call your own method from act()
- Place a method correctly inside the class and not inside another method
Words you will need
- Signature
- In plain words The first line of a method: what it gives back, its name, and what it needs.
- More precisely The method name together with its parameter list and return type.
- Body
- In plain words The code between the braces, which is what runs.
- More precisely The block of statements executed when the method is called.
- Define versus call
- In plain words Writing it down versus running it.
- More precisely A definition introduces the method; a call executes it.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A newly written void method in an editor with each part of the signature labeled.
public void turnAtEdge() { if (isAtEdge()) { turn(180); } }Read the first line against what you learned in 1.5.
`public` means anything can call it. Use it for now. `void` is the return type: this hands nothing back. `turnAtEdge` is the name you chose. `()` is empty, so it needs nothing from the caller.
Then the body in braces. That is the whole thing.
-
Step 2
On your screen The act method calling the new method by name.
public void act() { move(3); turnAtEdge(); }Writing a method does NOT run it. Something has to call it, and here that is act().
This is the most common first confusion: the method is written, compiled, correct, and nothing happens, because nothing ever calls it.
Note the empty parentheses on the call too. `turnAtEdge;` is not a call.
-
Step 3
On your screen A class diagram view showing act and turnAtEdge as sibling methods inside the same class.
public class Crab extends Actor { public void act() { move(3); turnAtEdge(); } public void turnAtEdge() { if (isAtEdge()) { turn(180); } } }Here is the placement rule, and it is the only hard part of this lesson.
Both methods are inside the CLASS braces and side by side. Neither is inside the other. Count the indentation: act and turnAtEdge start at the same depth.
-
Step 4
On your screen A method mistakenly nested inside act, with the compiler error visible below.
// WRONG. A method cannot live inside another method. public void act() { move(3); public void turnAtEdge() // illegal start of expression { } }This is what happens when you paste a method in without checking the braces. Java says `illegal start of expression`, which does not mention methods or braces at all.
The fix: make sure act() has been CLOSED before the next method starts. Re-indent the whole file; the point where the indentation stops making sense is where the missing brace belongs.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea Writing a method makes it run.
What is actually true Writing defines it. Something must call it by name.
Why it matters A correct, compiled, never-called method is the most common "why is nothing happening" in this unit.
Common wrong idea A method can go inside another method.
What is actually true Methods are siblings inside the class, never nested.
Why it matters The error Java gives for this does not mention methods, so it is very hard to diagnose without knowing the rule.
Common wrong idea You can call a method without the parentheses.
What is actually true The empty parentheses are required: turnAtEdge(), never turnAtEdge.
Why it matters Without them Java looks for a variable of that name and reports cannot find symbol.
Common wrong idea void methods are less useful than ones that return something.
What is actually true Most methods in this course are void. They do a job rather than answer a question.
Why it matters Chasing a return value where none is needed produces convoluted code.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. Where does a new method go?
-
2. You wrote a correct method and nothing happens when you run. What is most likely?
-
3. Which is a correct call to `public void reset()`?
-
4. Java reports `illegal start of expression` on the line where your new method begins. What is the likely cause?
Fill in the code
Write a method called resetToStart that puts the actor at the top left corner. Fill in the signature and the call.
public resetToStart()
{
setLocation(0, 0);
}
public void act()
{
}
Stuck? Open a hint for each blank
- Blank 1: It hands nothing back to whoever called it.
- Blank 2: Call the method above by name. Do not forget the parentheses and the semicolon.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. What does `void` in a method signature mean?
-
2. Which is a correctly written method definition?
-
3. Two methods in the same class should be:
-
4. Why is `turnAtEdge;` not a call?
The short version
- A method is public, a return type, a name, parentheses, and a body.
- Methods sit inside the class, side by side, never nested.
- Writing a method does not run it. Something must call it.
- A call always has parentheses, even when empty.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]