Static/instance members, constructors and object creation
Object-oriented programming models a problem through classes and objects, with clear responsibilities and controlled access to state.
What you need to be able to do
- DistinguishBetween static and non-static variables and methods
- ConstructCode to define classes and instantiate objects
Rapid Recall Deck
Say the answer aloud before flipping. Mark secure knowledge quickly and spend time on the gaps.
- What is an instance variable?: A variable whose value belongs to a particular object, so different objects can hold different values.
- What is a static or class variable?: A variable associated with the class itself and shared rather than separately stored for each object.
- When should an instance variable be preferred to a class variable?: When the value represents state that can differ from one object to another.
- What is a constructor used for?: It initializes a newly created object's state by assigning its initial attribute values.
- What does it mean to instantiate an object?: To create a concrete object from a class definition, usually by calling the class constructor.
Core knowledge and application
Between static and non-static variables and methods
Object-oriented programming models a problem through classes and objects, with clear responsibilities and controlled access to state.
Explain it without notes
Distinguish: Between static and non-static variables and methods in the context of a library-management application.
- Compare static and non-static variables and methods, including their usage and scope
- When to use instance variables instead of class variables, and how to apply these concepts effectively in code
Code to define classes and instantiate objects
Object-oriented programming models a problem through classes and objects, with clear responsibilities and controlled access to state.
Explain it without notes
Construct: Code to define classes and instantiate objects in the context of a library-management application.
- How to define classes and create objects from those classes
- Role of constructors in initializing an object's state, setting initial values for its attributes to define its condition or characteristics at the time of creation
Static vs instance state, constructors and objects
class Book:
library_name = "Central Library" # class/static-style data
def __init__(self, title):
self.title = title # instance data
self.on_loan = False
b1 = Book("Dune")
b2 = Book("Neuromancer")
b1.on_loan = Trueclass Book {
static String libraryName = "Central Library";
String title;
boolean onLoan;
Book(String title) {
this.title = title;
this.onLoan = false;
}
}
Book b1 = new Book("Dune");
Book b2 = new Book("Neuromancer");
b1.onLoan = true;Transfer to a new scenario
- Compare static and non-static variables and methods, including their usage and scope
- When to use instance variables instead of class variables, and how to apply these concepts effectively in code
- How to define classes and create objects from those classes
- Role of constructors in initializing an object's state, setting initial values for its attributes to define its condition or characteristics at the time of creation
Paper 2 practice
- Distinguish: Between static and non-static variables and methods in the context of a library-management application.
- Construct: Code to define classes and instantiate objects in the context of a library-management application.
Finish the learning cycle
IA — main task
Continue Criterion D development. Keep code readable, record meaningful implementation decisions, and maintain testing/evidence notes as you work.
Syllabus — short
Complete one targeted Paper 2/Paper 1 retrieval task from today’s lesson.