A class of your own
A class bundles values that belong together with the methods that work on them. Most classes in this course have had the same parts: private instance variables, a constructor, getters, methods that change the object, and a toString. Some also have an equals.
The tools
| Tool | What it does | Taught in |
|---|---|---|
public class Plank in the file Plank.java, with private instance variables |
a class of your own | module 17, Classes and objects |
a constructor, with this.length = length; |
sets up a new object | module 17, Classes and objects |
| a method that changes an instance variable | changes the object | module 17, Classes and objects |
getters such as getLength(), and toString() |
give values out, and the text println shows |
module 18, Getters, toString and bigger classes |
== on two objects |
are they the very same object? | module 22, Primitive and reference variables |
public boolean equals(Object compared) |
what "equal" means for this class | module 23, Objects working together |
A worked example
At a climbing gym, each wall has one route of each color, so a route is known by its wall and the color of its holds. It also has a grade, and the gym counts its ascents: how many times someone has climbed it to the top.
public class Main {
public static void main(String[] args) {
ClimbingRoute route = new ClimbingRoute("north wall", "red", 5);
route.climb();
route.climb();
System.out.println(route);
ClimbingRoute written = new ClimbingRoute("north wall", "red", 5);
System.out.println(route == written);
System.out.println(route.equals(written));
System.out.println(route.equals(new ClimbingRoute("north wall", "blue", 5)));
}
}public class ClimbingRoute {
private String wall;
private String color;
private int grade;
private int ascents;
public ClimbingRoute(String wall, String color, int grade) {
this.wall = wall;
this.color = color;
this.grade = grade;
this.ascents = 0;
}
public String getWall() {
return this.wall;
}
public String getColor() {
return this.color;
}
public int getGrade() {
return this.grade;
}
public int getAscents() {
return this.ascents;
}
public void climb() {
this.ascents++;
}
public boolean equals(Object compared) {
if (this == compared) {
return true;
}
if (!(compared instanceof ClimbingRoute)) {
return false;
}
ClimbingRoute other = (ClimbingRoute) compared;
return this.wall.equals(other.wall) && this.color.equals(other.color);
}
public String toString() {
return this.color + " route on the " + this.wall + ", grade " + this.grade + ", ascents: " + this.ascents;
}
}red route on the north wall, grade 5, ascents: 2
false
true
false- The constructor's parameters have the same names as the instance variables.
this.wallis the object's variable, andwallalone is the parameter (module 18). climb()returns nothing: it changes the object's state. The getters only hand values out.route == writtenis false: twonews make two objects, whatever they hold.equalscompares what's in them, in the way the class decides.- The
equalsis module 23's, step by step: the exact headerpublic boolean equals(Object compared), the check for the very same object, theinstanceofcheck, the cast, and then the comparison. Strings are compared withequals, and numbers with==. - This
equalscompares only the wall and the color, the values that tell routes apart. The ascents change every day, and they don't make it another route. - The list methods
contains,indexOfandremove(value)useequalstoo (module 23), so a list of routes can find a route by its wall and color.
Module 32 comes back to equals, and adds its partner, hashCode.