Beginner 30 min read

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 + ')';
    }
}
  1. public class Student { — the class.
  2. private String studentId; — private — nothing outside Student can silently overwrite this once it's set.
  3. private String name; — same protection.
  4. private String email; — same protection.
  5. (blank line)
  6. public Student(String studentId, String name, String email) { — the constructor — the only place these fields are ever set.
  7. this.studentId = studentId; — sets the id.
  8. this.name = name; — sets the name.
  9. this.email = email; — sets the email.
  10. } — closes the constructor.
  11. (blank line)
  12. public String getStudentId() { — a controlled, read-only window onto the id.
  13. return studentId; — returns it.
  14. } — closes the method.
  15. (blank line)
  16. public String getName() { — a controlled, read-only window onto the name.
  17. return name; — returns it.
  18. } — closes the method.
  19. (blank line)
  20. public String describe() { — a human-readable summary.
  21. return name + ' (' + studentId + ')'; — combines both fields into one string.
  22. } — closes the method.
  23. } — 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)"
  1. Student alice = new Student('S001', 'Alice Nkosi', 'alice@example.com'); — creates the first student, running the constructor.
  2. Student ben = new Student('S002', 'Ben Dlamini', 'ben@example.com'); — creates a second, completely independent student.
  3. (blank line)
  4. System.debug(alice.describe()); — logs "Alice Nkosi (S001)".
  5. 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; }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Model a Student Quiz

1. Why are studentId, name, and email declared private on Student?

Log in to submit the quiz and save your score.

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.