3.6 The World Constructor
The Constructor in a Greenfoot World
Unit 3: Methods, Worlds, and Constructors · Lesson 3.6 · about 25 minutes · no coding experience needed
Every time you press Reset, Greenfoot throws your world away and builds a brand new one. The constructor is the code that runs during that build, which makes it the answer to the question Unit 1 left hanging.
What you will be able to do
- Recognize a constructor and say how it differs from a method
- Explain when a constructor runs
- Explain why super must be the first statement
Words you will need
- Constructor
- In plain words The setup code that runs once when the object is made.
- More precisely A special member with the class name and no return type, invoked by new.
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 world constructor with its name matching the class name, both highlighted.
public class OceanWorld extends World { public OceanWorld() { super(20, 15, 30); } }Two things mark this out as a constructor rather than a method.
Its name is EXACTLY the class name, capital letter and all. It has NO return type. Not void, not int. Nothing at all between public and the name.
If you write `public void OceanWorld()` you have quietly made an ordinary method that never runs, and Java will not warn you.
-
Step 2
On your screen The Greenfoot Reset button being pressed and a fresh world appearing.
When does it run? Once, when the world is created. In Greenfoot that means when the scenario opens and every single time you press Reset.
You never call it yourself. Creating the object runs it for you, exactly like act().
This is why hand-placed actors vanish on Reset: the new world runs its constructor, and the constructor knows nothing about actors you dragged in.
-
Step 3
On your screen A constructor with super first followed by other setup lines.
public OceanWorld() { super(20, 15, 30); // MUST be first setPaintOrder(Fish.class); prepare(); }super must be the FIRST statement. Java enforces this, and the reason is sensible: the inherited World part of your object has to exist before anything else touches it. There is no world to set a paint order on until super has run.
Everything else follows. Here the constructor sets a paint order and then calls prepare(), which is the next lesson and where the actors get placed.
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 A constructor needs a return type like void.
What is actually true It has NO return type. Adding void makes it an ordinary method that never runs.
Why it matters This produces a world that ignores all your setup, with no error message at all.
Common wrong idea You call the constructor yourself.
What is actually true Creating the object calls it. `new OceanWorld()` runs it.
Why it matters Same confusion as act(): the framework calls it, you only write it.
Common wrong idea super can go anywhere in the constructor.
What is actually true It must be the first statement, and Java refuses otherwise.
Why it matters Knowing the rule beats decoding the error, which talks about "call to super must be first statement in constructor".
Common wrong idea The constructor runs every frame.
What is actually true Once, at creation. act() is the one that runs every frame.
Why it matters Confusing the two is what puts spawning code in the wrong place and either floods the world or never spawns at all.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. How do you recognize a constructor?
-
2. When does a world constructor run?
-
3. What is wrong with `public void OceanWorld()` in class OceanWorld?
-
4. Where must the super call go?
Fill in the code
Complete this constructor. The class is called MazeWorld and the grid is 15 by 15 cells at 32 pixels.
public class MazeWorld extends World
{
public ()
{
(15, 15, 32);
}
}
Stuck? Open a hint for each blank
- Blank 1: A constructor is named after its class, exactly.
- Blank 2: The call that sets up the inherited World part. It must come first.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. A constructor differs from a method because it:
-
2. Pressing Reset in Greenfoot:
-
3. Why must super come first?
-
4. Spawning code that should run once per game belongs in:
The short version
- A constructor has the class name and NO return type.
- It runs once, when the object is created. Reset creates a new world.
- You never call it. new does.
- super must be the first statement.
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]