A class is defined as a blueprint containing attributes and methods. An object is instantiated by calling the class's constructor, usually with the new keyword in Java-style syntax.
Defining a class
In object-oriented programming (OOP), a class groups related data and operations. Its attributes represent an object's state, while its methods define the object's behaviour.
class Student {
private String name;
private int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
int getScore() {
return score;
}
}
Here, Student is the class. The variables name and score are instance attributes. The private access modifier supports encapsulation by preventing direct access from outside the class.
The method named Student is the constructor. It runs when an object is created and initializes that object's attributes. The keyword this refers to the current object, so this.name = name assigns the constructor parameter to the object's name attribute.
Instantiating an object
Student learner = new Student("Maya", 84);
This statement performs three key steps:
new Student("Maya", 84)creates an instance ofStudent.- The constructor initializes its state as
name = "Maya"andscore = 84. learnerstores a reference to the new object.
A second statement such as Student learner2 = new Student("Luis", 91); creates a separate object with its own state. Both objects share the class's structure and method definitions, but their attribute values can differ.
Exam technique
For IB Computer Science topic B3.1, clearly distinguish the class from its instances: the class is the blueprint, whereas an object is one concrete instance. A common misconception is that declaring a reference variable alone creates an object; actual instantiation requires a constructor call such as new Student(...). In code-writing questions, include the class name, attributes, constructor, methods and a correctly typed object reference.