Parts of a Java Class

Suppose we open the file Student.java to find the following code snippet:

public class Student {
  private String name;
  private String email;

  public Student(String name, String email) {
    this.name = name;
    this.email = email;
  }

  public String getName() {
    return name;
  }

  public String getEmail() {
    return email;
  }
}

Exercise Identify the parts of the Student class: fields, constructor, methods.

Solution

Parts of the Student class:

  • Fields (instance variables)
    private String name;
    private String email;
    
  • Constructor
    public Student(String name, String email) {
      this.name = name;
      this.email = email;
    }
    
  • Methods
    public String getName() {
      return name;
    }
    
    public String getEmail() {
      return email;
    }
    
Resources