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:
-
Reflexive: Object must be equal to itself.
-
Symmetric: if a.equals(b) is true then b.equals(a) must be true.
-
Transitive: if a.equals(b) is true and b.equals(c) is true then c.equals(a) must be true.
-
Consistent: several calls to the equals() method must return the same value (unless a property has been modified on one object).
-
Null comparison: compare any object to null must not result in a NullPointerException.
For the hashcode()
method:
-
when two objects are equal by
equals()
theirhashcode()
returned value must be same. -
when two objects are not equal by
equals()
theirhashcode()
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:
-
Java Architect creates, in the model, the two overriden methods
equals()
andhashcode()
along with their implementation code. -
The code generated by Java Architect takes care of all the attributes/associations of the class to compute equality and hash code
-
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
![]() |
The selected 'subject' class must be modifiable in the model. |
-
In the model, select the existing class which you want to create the override methods.
-
From the contextual menu (rightclick) execute the
Java Architect →
Create element →
Standard methods command.
-
The command will immediately create
equals()
,hashcode()
andtoString()
methods on the selected class along with their proper implementations -
Generate the java code for the class.
Automatic update of the generated standard methods
Once the previous Java Architect →
Create element →
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.