Java OOP · Module 1
State vs Behaviour
Attributes are what an object *is*, methods are what it *does*
What you'll be able to do
- Split any real-world thing into state and behaviour
- Turn that split into fields and methods of a Java class
- Explain why each object carries its own state
- Name fields and methods the way professional Java code does
State vs Behaviour
State changes, behaviour changes it
If you can describe a thing with 'it has...' and 'it can...', you can already design a class.
State is everything an object currently knows about itself: a car's speed and fuel level, an account's balance, a phone's on/off flag. Behaviour is everything the object can do, and behaviour is usually the *only* legitimate way state should change.
Read that last sentence twice — it is the seed of encapsulation in Module 4. car.speed = 300; from the outside is a lie about physics; car.accelerate(30) is a request the object can validate.
The dashboard and the pedals
In a car, the dashboard shows state (speed 60, fuel 40%) and the pedals are behaviour (accelerate, brake). You never edit the speedometer with a screwdriver to go faster — you press a pedal and the car recalculates its own state. A well designed class works the same way.
Car: state on top, behaviour below
void because it performs an action instead of answering a question.Call `accelerate(20)` twice before the dashboard and predict both speed and fuel before you run it.
Attributes vs Methods
Attributes and methods: the vocabulary
Words that mean almost the same thing (and why)
From a sentence to a class
| Real-world sentence | Becomes | Java |
|---|---|---|
| A student has a name | attribute | String name; |
| A student has an age | attribute | int age; |
| A student can introduce themselves | behaviour | void introduce() { ... } |
| An account has a balance | attribute | double balance; |
| An account can accept a deposit | behaviour | void deposit(double amount) |
| An account can report its balance | behaviour that answers | double getBalance() |
'has' → field. 'can' → method. That mapping alone will carry you through the first half of this course.
Name fields as nouns, methods as verbs
balance, owner, speed — nouns. deposit(), accelerate(), printStatement() — verbs. Methods that answer a question start with get/is: getBalance(), isActive(). Following this convention makes your code readable to every Java developer on earth.
Common mistakes
car.speed = 300;Declaring `static double fuel;` in Car.Naming a method `speed()` when it changes the speed.Lesson playground
Two accounts, one class. Deposit into only one of them and prove that the other is untouched.
Check understanding
Quiz
Question 1 of 4
Each object keeps its own state. What is printed?
Summary
- State = fields (what the object knows). Behaviour = methods (what it can do).
- 'has' → field, 'can' → method: that is how you translate a requirement into a class.
- Behaviour should own state changes so related fields stay consistent.
- Each object has its own state; `static` would make a field shared by the whole class.