Programming technique · B3.1.3
Static and instance members
Class and instance members
Instance members belong to one object. Static members belong to the class as a whole and are shared rather than copied into every instance.
Instance attributes belong to one object. Class attributes and class methods belong to the class as a whole and are shared rather than copied into every instance.
One class-owned value, separate object state
RegistryUnit class
Shared once: createdCountcreated_count
Each object
first
- id
- 1
- name
- Ari
- energy
- 8
second
- id
- 2
- name
- Sol
- energy
- 12
+ createdCount: int {static}+ id: int+ name: String+ energy: int + RegistryUnit(name, energy)+ summary(): String+ getCreatedCount(): int {static} + created_count: int {class}+ id: int+ name: str+ energy: int + __init__(name, energy)+ summary(): str+ get_created_count(): int {class} Java ownership model
Python ownership model
Distinguish members by responsibility
| Question | Instance member | Class-owned member |
|---|---|---|
| Owner | One object | The class as a whole |
| Stored values | Normally one per object | One shared value |
| Typical access | first.summary() | RegistryUnit.getCreatedCount()RegistryUnit.get_created_count() |
| Suitable use | Identity or state that differs between objects | A genuine responsibility shared by the class |
Class-owned does not mean constant
The shared count changes while remaining owned by the class.
Objects keep independent state
Ari and Sol must not share one name or energy value.
Use the class name
Shared state is clearest when accessed through RegistryUnit, not an arbitrary object.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Registry Unit Counter
SelectedComplete a RegistryUnit class with one class-owned created count and independent id, name and energy fields for every object. The constructor must update the shared count and assign the new value as the object's id. Create at least three objects, trace the count after each construction and distinguish the receiver-based summary method from the class-owned count reader.
Dice Roll Statistics
SelectedCreate a Dice class in which each object stores its own number of sides and current value, while one static totalRolls field counts rolls made by every Dice object. Roll dice of different sizes, display their independent values and prove that the shared total belongs to the class rather than one die.
Club Membership Counter
SelectedCreate a ClubMember class with independent memberId, name and yearGroup fields and one class-owned memberCount. Give each new object the next id during construction. Add an instance summary method and a static count reader, then explain why names and year groups must not be static.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.