The so-called standard Java methods in Java Architect are the equals(), hashCode() and toString() methods which are defined a the java.lang.Object class level and very often redefined for Object sub-classes.

The toString() method is usually redefined as a convenience method to help tracing object when debugging. It should not be used as a formatting method that makes a String from an object.

The equals() and hashCode() are more critical and deserve some detailed explanation.

The equals() and hashcode() methods

In Java equals() and hashCode() are two essentials methods declared in the java.lang.Object class.

The equals() method is used to compare Objects for equality. The hashCode() method is used to generate an integer hashcode number corresponding to an object. Both equals() and hashCode() are used in Java core library for example in 'hash-based' and 'set-based' containers.

The Java default implementation of equals() in java.lang.Object compares memory location and returns true if two variable are referring to the same memory location i.e. they are strictly the same object. The default implementation of hashcode() is (approximately) the reference itself.

However, these methods may need to be overridden. A typical example is the String class implementation of equals() which returns true when two String instances contains the same characters. Situations where equals() and hascode() should better be redefined by via some business logic are not so uncommon.

Overriding equals() and hashcode() must follow strict rules in order to be consistent with their usage in the Java core library (containers).

Without entering into too much details here are some properties that an overriden implementation of equals() must have:

  1. Reflexive: Object must be equal to itself.

  2. Symmetric: if a.equals(b) is true then b.equals(a) must be true.

  3. Transitive: if a.equals(b) is true and b.equals(c) is true then c.equals(a) must be true.

  4. Consistent: several calls to the equals() method must return the same value (unless a property has been modified on one object).

  5. Null comparison: compare any object to null must not result in a NullPointerException.

For the hashcode() method:

  1. when two objects are equal by equals() their hashcode() returned value must be same.

  2. when two objects are not equal by equals() their hashcode() returned value can be same or different.

After this basic theory considerations, implementing overidden equals() and hashcode() methods may appear to be a little bit tricky.

Java Architect can help you to implement reliable equals() and hashcode() override:

  1. Java Architect creates, in the model, the two overriden methods equals() and hashcode() along with their implementation code.

  2. The code generated by Java Architect takes care of all the attributes/associations of the class to compute equality and hash code

  3. The generated methods are updated each time the model changes: adding/removing properties, modifying types and so on. This ensures that the method implementations will always remain coherent with the real definition of the class and its properties.

Creating the standard methods

The pattern can be applied either to a Class. For the selected class the pattern will create:

  • an equals() method

  • an hashcode() method

  • a toString() method

Important The selected 'subject' class must be modifiable in the model.
  1. In the model, select the existing class which you want to create the override methods.

  2. From the contextual menu (rightclick) execute the module Java Architect → creationgroup Create element → automethod Standard methods command.

  3. The command will immediately create equals(), hashcode() and toString() methods on the selected class along with their proper implementations

  4. Generate the java code for the class.

Automatic update of the generated standard methods

Once the previous module Java Architect → creationgroup Create element → automethod Standard methods command has been run, the created methods are monitored by Java Architect so that any change to the properties of their class can be detected and taken into account to update the code of the generated methods.

This automatic update of standard methods feature can be globally enabled/disabled in the Java Architect module preferences.