.equals
Objects are reference type variables and therefore store the memory address of the object. So, if we try to compare two objects for equality using ==, the compiler will compare their memory addresses for equality instead of their values. For this reason, a .equals method should be provided when writing a class. The class designer should decide what makes two objects of the same class equal. For example, two Circles may be equal if they have the same center and radius OR two MusicNotes may be equal if they are the same pitch AND duration.
The signature of all .equals methods should be public boolean equals(Object o). Note that the method takes an Object parameter. Inside the method, that parameter will need to be cast to the class type. Before casting, the instanceof operator needs to be used to make sure that o is an instance of the class in which the code is being written.
public boolean equals(Object o){
if( o instanceof Dog){
Dog d = (Dog)o;
return this.getName().equals(d.getName()) && this.getCage()==d.getCage();
}
return false; //was not an instance of the Dog class
}
.compareTo
For the same reasons that we cannot use == to compare equality, we cannot use > or < for ordering. So, a .compareTo method has to be written for ordering. The compareTo method returns an int: positive if the object is greater than the parameter, 0 if they are equal and negative if the object is less than the parameter. A typical condition of the compareTo method is that two objects deemed equal by .equals should be deemed equal by compareTo. Finally, the compareTo method allows the class type to be the parameter, as opposed to the Object type.
public int compareTo(Dog d){
//order dogs by cage
return this.getCage() - d.getCage();
}
Other Methods
Classes often require other methods besides getters, setters, constructers, equals and compareTo. This is what makes the class useful in a program. Examples:
- A Car class might need methods for stop(), turn() and drive().
- A TrafficLight class might need a method called change() that is designed to set the color in the proper order.
- A MusicNote class might need a method called play() that plays the note.
Feel free to write whatever methods are needed to make your classes useful.
Static Keyword
The word static means unchanging. In java, static is used to describe methods that do not access/alter/change private instance variables. For example, the Math class has a variety of methods that take input and alter that input, without storing any information in private instance variables. These Math methods are therefore static and called from the class name. The String methods, however, require a String type variable on which to operate. So, the String methods are mostly non-static and are called from a variable name.
The static keyword can also be applied to variables within a class. When creating class constants, that users may want to access outside of a class, they should be declared as static. For example, Math.PI is a class constant with a value of 3.1415.... Static variables that are not constants, are known as class variables and are not treated as private values of an object. They instead stay with the class. An example of their use is to create a self incrementing id number that can be automatically assigned to objects when created.
public class Computer{
private static int id=1;
private int myId;
private String model;
public Computer()
{
model="Dell";
myId=id;
id++;
}
}
Using the code above:
Computer c1=new Computer(); //has an id of 1
Computer c2=new Computer(); //has an id of 2
//the static id variable is currently set to 3