1.3 Classes and Objects
Classes and Objects in Greenfoot
Unit 1: Meet Greenfoot · Lesson 1.3 · about 30 minutes · no coding experience needed
This is the idea that unlocks everything else, and it is worth slowing down for. A recipe is not a cake. You can bake twelve cakes from one recipe, eat one, burn another, and the recipe is unchanged. In Java, the recipe is a CLASS and each cake is an OBJECT.
What you will be able to do
- Explain the difference between a class and an object in your own words
- Create several objects from one class and predict what happens to each
- Read `new Crab()` and say what it produces
Words you will need
- Class
- In plain words The plan for a kind of thing. Written once.
- More precisely A class defines the state and behavior that every object of that type will have.
- Object
- In plain words One actual thing built from the plan.
- More precisely An instance of a class, with its own copy of the state the class describes.
- Instantiate
- In plain words Make one.
- More precisely Create a new object from a class, written `new ClassName()` in Java.
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 Right-click menu on the Crab class in the class diagram, with the new Crab() option highlighted.
Right-click the Crab class and Greenfoot offers you `new Crab()`. Read that as an instruction: "make me one crab". The class is the plan; you are asking for one thing built from it.
-
Step 2
On your screen Three separate crab actors placed at different positions in the world.
Do it three times and place them apart. Three objects. One class. Nothing about the Crab class changed when you made them, and nothing about it changes if you delete them.
-
Step 3
On your screen One crab dragged to a new position while the other two remain where they were.
Now drag one crab somewhere else. The other two do not follow. This is the whole point: each object carries its own position. They share behavior, because that comes from the class. They do not share state.
-
Step 4
On your screen The Greenfoot object bench below the world, holding a crab object created for inspection.
Crab myCrab = new Crab();In code the same request looks like this. `new Crab()` builds one crab. `Crab myCrab =` gives that particular crab a name so you can talk about it later. You are not writing this line yet; you are learning to read it, because it shows up constantly from Unit 3 onwards.
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 There is one crab, and the class IS the crab.
What is actually true The class is the plan. There can be zero crabs, one, or fifty.
Why it matters A student holding this idea cannot make sense of a game with twenty enemies, because they think twenty enemies needs twenty classes.
Common wrong idea Changing one object changes all objects of that class.
What is actually true Each object has its own state. Moving one moves one.
Why it matters This is what makes a score-per-enemy or a health-per-player possible at all.
Common wrong idea Deleting an object deletes the class.
What is actually true The plan survives. You can always make another.
Why it matters Removing an enemy when it is hit must not remove the ability to spawn the next one.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. A scenario has one Bubble class. You place seven bubbles in the world. How many classes and how many objects are there?
-
2. You move one of the seven bubbles to the top of the world. What happens to the other six?
-
3. What does `new Fish()` do?
-
4. Which is the better analogy for a class?
Fill in the code
A scenario needs three fish and one shark placed in an aquarium. Fill in the class names so that each line builds the right kind of thing.
Fish fishOne = new ();
Fish fishTwo = new ();
Fish fishThree = new Fish();
Shark bigOne = new ();
Stuck? Open a hint for each blank
- Blank 1: What kind of thing is fishOne? The type on the left tells you.
- Blank 2: The same plan builds every one of them.
- Blank 3: This one is not a fish.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. Which statement is TRUE?
-
2. A game has 30 identical asteroids on screen. How many Asteroid classes does it most likely have?
-
3. In `Crab myCrab = new Crab();`, which part actually builds the object?
-
4. You delete every crab from the world. What happens to the Crab class?
The short version
- A class is the plan. An object is one thing built from that plan.
- `new Crab()` builds one crab.
- Objects share behavior, because that comes from the class.
- Objects do NOT share state. Each keeps its own position.
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]