Model a Student
By the end of this lesson, you'll be able to:
- Write the Student class with encapsulated fields and a constructor
- Add a method that returns a readable description of a student
- Create and use multiple Student objects
Prerequisites: "Design the Classes"
The Student class
public class Student {
private String studentId;
private String name;
private String email;
public Student(String studentId, String name, String email) {
this.studentId = studentId;
this.name = name;
this.email = email;
}
public String getStudentId() {
return studentId;
}
public String getName() {
return name;
}
public String describe() {
return name + ' (' + studentId + ')';
}
}
public class Student {— the class.private String studentId;—private— nothing outsideStudentcan silently overwrite this once it's set.private String name;— same protection.private String email;— same protection.- (blank line)
public Student(String studentId, String name, String email) {— the constructor — the only place these fields are ever set.this.studentId = studentId;— sets the id.this.name = name;— sets the name.this.email = email;— sets the email.}— closes the constructor.- (blank line)
public String getStudentId() {— a controlled, read-only window onto the id.return studentId;— returns it.}— closes the method.- (blank line)
public String getName() {— a controlled, read-only window onto the name.return name;— returns it.}— closes the method.- (blank line)
public String describe() {— a human-readable summary.return name + ' (' + studentId + ')';— combines both fields into one string.}— closes the method.}— closes the class.
This is exactly the encapsulation pattern from Module 11: private fields, set once through the constructor, exposed through controlled public getter methods.
Creating students
Student alice = new Student('S001', 'Alice Nkosi', 'alice@example.com');
Student ben = new Student('S002', 'Ben Dlamini', 'ben@example.com');
System.debug(alice.describe()); // "Alice Nkosi (S001)"
System.debug(ben.describe()); // "Ben Dlamini (S002)"
Student alice = new Student('S001', 'Alice Nkosi', 'alice@example.com');— creates the first student, running the constructor.Student ben = new Student('S002', 'Ben Dlamini', 'ben@example.com');— creates a second, completely independent student.- (blank line)
System.debug(alice.describe());— logs"Alice Nkosi (S001)".System.debug(ben.describe());— logs"Ben Dlamini (S002)"— each object's own data, exactly as the object model from Module 10 predicts.
Exercise
Add a getEmail() method to the Student class and create one Student object, debugging their email.
Show hint
public String getEmail() { return email; }
Model a Student Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson turns the Student sketch from Lesson 1 into a real class — encapsulated fields, a constructor, and a describe method, exactly as planned.