Parts of a Java Class
- Identify 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
- For a quick refresher on Java's syntax, refer to this awesome Java Programming Cheatsheet.