Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 1

State vs Behaviour

Attributes are what an object *is*, methods are what it *does*

13m35 XP
→→→

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
1

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

Main.java
1class Car {
2 String model;
3 double fuel;
4 int speed;
5
6 void accelerate(int amount) {
7 speed = speed + amount;
8 fuel = fuel - 0.5;
9 }
10}
String model;
State: identity of this particular car. It rarely changes.
double fuel;
State: a value that behaviour consumes. Notice it is a double because half litres exist.
int speed;
State that changes constantly — which is exactly why a method should own the change.
void accelerate(int amount) {
Behaviour. void because it performs an action instead of answering a question.
speed = speed + amount;
The method reads and updates *its own object's* fields. Two cars accelerating never interfere with each other.
fuel = fuel - 0.5;
One action can change several fields at once and keep them consistent — something loose variables cannot guarantee.
Drive the object

Call `accelerate(20)` twice before the dashboard and predict both speed and fuel before you run it.

Output
Press Run to execute this example.
2

Attributes vs Methods

Attributes and methods: the vocabulary

Words that mean almost the same thing (and why)

Field
The Java word for a variable declared inside a class.
Attribute / property
The design word for the same thing. Interviewers use all three interchangeably.
Instance variable
A field that each object owns a private copy of — the normal case.
Method
Behaviour that belongs to the class; when called on an object it can read and change that object's fields.
Member
Umbrella term for fields + methods together.

From a sentence to a class

Real-world sentenceBecomesJava
A student has a nameattributeString name;
A student has an ageattributeint age;
A student can introduce themselvesbehaviourvoid introduce() { ... }
An account has a balanceattributedouble balance;
An account can accept a depositbehaviourvoid deposit(double amount)
An account can report its balancebehaviour that answersdouble 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

Wrongcar.speed = 300;
WhyOutside code decided the state directly, so no rule could check it and `fuel` was not updated — the object is now inconsistent.
Fixcar.accelerate(30); — let the object update its own state consistently.
WrongDeclaring `static double fuel;` in Car.
Why`static` makes the field belong to the class, so *every* car would share one fuel tank.
FixLeave instance fields non-static; each object needs its own state.
WrongNaming a method `speed()` when it changes the speed.
WhyA noun name suggests it returns a value, so callers misuse it.
FixUse `accelerate()` / `setSpeed()` for actions and `getSpeed()` for answers.

Lesson playground

Free practice

Two accounts, one class. Deposit into only one of them and prove that the other is untouched.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

Each object keeps its own state. What is printed?

Main.java
1class BankAccount {
2 String owner;
3 double balance;
4
5 void deposit(double amount) {
6 balance = balance + amount;
7 }
8}
9
10public class Main {
11 public static void main(String[] args) {
12 BankAccount a = new BankAccount();
13 a.owner = "Sara";
14 a.deposit(100);
15 a.deposit(50.5);
16
17 BankAccount b = new BankAccount();
18 b.owner = "Omar";
19
20 System.out.println(a.owner + " " + a.balance);
21 System.out.println(b.owner + " " + b.balance);
22 }
23}

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.