Skip to content
Related Articles

Related Articles

Improve Article
Method Class | equals() Method in Java
  • Last Updated : 05 Dec, 2018

The java.lang.reflect.Method.equals(Object obj) method of Method class compares this Method Object against the specified object as parameter to equal(object obj) method. This method returns true if Method object is same as passed object. Two Methods are the same if they were declared by the same class and have the same name and formal parameter types and return type.

Syntax:

public boolean equals(Object obj)

Parameter: This method accepts a mandatory parameter obj which is the object to be compared.

Return Value: The method return true if Method object is same as passed object as parameter, otherwise false.

Below program illustrates equals(Object obj) method of Method class:



Examples 1: When both objects are same.




/*
* Program Demonstrate equals(Object Obj)
* method of Method Class.
*/
import java.lang.reflect.Method;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchMethodException,
               SecurityException,
               ClassNotFoundException
    {
  
        // Get all method of class GFGClass
        // and store in an Array of Method Objects
        Method[] methods = GFGClass.class.getMethods();
  
        // create a method object by passing
        // method name and parameter type
        Method method = GFGClass.class
                            .getMethod("add", String.class);
  
        System.out.println("First Method object from array "
                           + "create by getMethods():");
        System.out.println(methods[0]);
        System.out.println("Method object created by "
                           + "getMethod():");
        System.out.println(method);
  
        // compare both objects by equals method
        if (methods[0].equals(method)) {
            System.out.println("Both Method objects"
                               + " are equal");
        }
        else {
            System.out.println("Both Method objects"
                               + " are not equal");
        }
    }
}
  
// This is Sample Class for which
// the class object are created
class GFGClass {
  
    // We have two variables in this class
  
    private String field1;
  
    public GFGClass()
    {
    }
  
    // creatin methods
    public String add(String field2)
    {
        return this.field1 + field2;
    }
}
Output:
First Method object from array create by getMethods():
public java.lang.String GFGClass.add(java.lang.String)
Method object created by getMethod():
public java.lang.String GFGClass.add(java.lang.String)
Both Method objects are equal

Example 2: When both objects are not same.




/*
* Program Demonstrate equals(Object Obj) method of Method Class.
*/
import java.lang.reflect.Method;
  
// This is Main Class
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Get all method of class GFGClass
        // and store as Array of Method Objects
        Method[] methods = GFGClass.class.getMethods();
  
        // select any two method and compare
        System.out.println("Methods are :");
        System.out.println(methods[0]);
        System.out.println(methods[1]);
        if (methods[0].equals(methods[1])) {
            System.out.println("Methods are equal");
        }
        else {
            System.out.println("Methods are not equal");
        }
    }
}
  
// This is Sample Class
class GFGClass {
  
    // We have two variables in this class
    private String field1;
    private String field2;
  
    public GFGClass()
    {
    }
  
    // we have four methods in this class
    public String getField1()
    {
        return field1;
    }
  
    public void setField1(String field1)
    {
        this.field1 = field1;
    }
  
    public String getField2()
    {
        return field2;
    }
  
    public void setField2(String field2)
    {
        this.field2 = field2;
    }
}
Output:
Methods are :
public java.lang.String GFGClass.getField1()
public void GFGClass.setField1(java.lang.String)
Methods are not equal

Reference:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#equals-java.lang.Object-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :