Constructor equals() method in Java

The java.lang.reflect.Constructor class defines many methods that provide access and information about a single constructor of a class. The equals() method in the constructor class in Java is used to check whether this constructor equals the passed object or not. Two constructor objects are said to be equal if they belong to the same class and have the same formal parameter types.

The syntax for this method can be given as follows:

public boolean equals(Object obj);

The argument for this method is an object that specifies the object with which we compare this object. The method returns true if the objects are the same otherwise it returns false.

Let us see a Java program that will explain how we can use equals() method.

import java.lang.reflect.Constructor;
import java.util.Vector;

class Example{
  public static void main (String[] args) {
      Constructor constructor_obj_arr[] = Vector.class.getConstructors();
      
      System.out.println("The first element in constructor object array is : " + constructor_obj_arr[0]);
      System.out.println("The second element in constructor object array is : " + constructor_obj_arr[1]);
      
      //comparing first and second element in constructor object array 
      boolean ret =  constructor_obj_arr[0].equals(constructor_obj_arr[1]);
      
      if(ret == true)
          System.out.println("The constructor objects are equal.");
      else
          System.out.println("The constructor objects are not equal.");
  }
}

Output:

The first element in constructor object array is : public java.util.Vector(java.util.Collection)
The second element in constructor object array is : public java.util.Vector()
The constructor objects are not equal.

In the above program, we have created a class object using Vector class and stored its constructors in an array as shown in the program. As you can see, when we print the first two elements, the parameters are different. Therefore, these constructor objects are not equal. Hence, the equals() method return false when we compare the first and second element of the constructor object array.

In the following example, the equals() method returns true as we are passing a copy of the same object as the argument. have a look at the program and its output.

import java.lang.reflect.Constructor;
import java.util.Vector;

class Example{
  public static void main (String[] args) {
      Constructor constructor_obj_arr[] = Vector.class.getConstructors();
      
      System.out.println("The first element in constructor object array is : " + constructor_obj_arr[0]);
      
      Object obj = constructor_obj_arr[0];
      
      System.out.println("The object to compare is : " + obj);
      
      //comparing first element in constructor object array and obj
      boolean ret =  constructor_obj_arr[0].equals(obj);
      
      if(ret == true)
          System.out.println("The constructor objects are equal.");
      else
          System.out.println("The constructor objects are not equal.");
  }
}

Output:

The first element in constructor object array is : public java.util.Vector(java.util.Collection)
The object to compare is : public java.util.Vector(java.util.Collection)
The constructor objects are equal.

Thank you.

Also read: Constructor Overloading in Java

Leave a Reply

Your email address will not be published. Required fields are marked *