Creating and Referencing Objects

Object variables are called references because they don’t actually store an object but reference it, as opposed to primitive type variables.

You create an object from a class (that provides kind of a blueprint for it) using the new operator, which:

  • Instantiates a class, i.e. invokes a class constructor (the one who matches the arguments) to create a new object by allocating memory for it
  • Returns a reference to the created object. Using that reference you can access the object fields or call its methods:

Assigning another value to the reference variable will dereference the object (the object will still be there but there will be no way to access it):

Removing Objects

In Java you can’t explicitly remove objects, but instead Java itself is responsible for removing objects that are no longer needed. The Java Virtual Machine (JVM) carries out this task by using a feature called Garbage Collection.

An object is eligible for garbage collection when there are no more references to it. The reference held in a variable is usually automatically dropped when it goes out of scope but you can also explicitly drop an object reference by setting the variable to null.

Pay attention to the word eligible: nothing can ensure that an object will definitely be destroyed by the garbage collector (GC). In fact you can invoke one of the following methods (they are equivalent) to request the JVM to perform garbage collection, but there is no guarantee that it will do it:

  • System.gc();
  • Runtime.getRuntime.gc();

Object as a superclass

Every class is a descendant, direct or indirect, of the Object class and that means that all Java classes inherit its methods:

Object main methods
public boolean equals(Object obj) See below
public int hashCode() See below
public String toString() Default implementation returns "className@hashCode"
public final Class<?> getClass() Returns the class of this object
protected Object clone() throws CloneNotSupportedException Creates and returns a copy of this object
protected void finalize() throws Throwable Called by the GC on an object when it determines that there are no more references to the object

The equals – hashCode Contract

The equals method in Object uses the identity operator (==) to determine if two objects are equal. For primitive data types this gives the correct result but for objects it doesn’t (it tests whether the object references are the exact same object). You should always override the equals method if the identity operator is not appropriate for your class.

The hashCode method in Object returns the object’s hash code, which by default is the object’s memory address in hexadecimal.

By definition if two objects are equal, they must have the same hash code. Therefore, if you override the equals method, you should also override the hashCode method. Note that the opposite is not needed: if two objects have the same hash code, they may or may not be equal (although producing distinct hash codes for unequal objects may improve the performance of hash tables).

Leave a Reply