Method Class | equals() Method in Java Last Updated : 27 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the Method object is the same as the 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 of equals() Methodpublic boolean equals(Object obj) Parameter This method accepts a mandatory parameter obj which is the object to be compared. Return Value The method returns true if the Method object is the same as the passed object as a parameter, otherwise false. Examples of equals() method The below program illustrates equals(Object obj) method of Method class: Examples 1: When both objects are the same. Java // Java Program Demonstrate // equals(Object Obj) method // of Method Class. import java.lang.reflect.Method; // Driver class public class GFG { // main function 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() {} // creating methods public String add(String field2) { return this.field1 + field2; } } OutputFirst 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.Strin... Example 2: When both objects are not the same. Java // Program Demonstrate // equals(Object Obj) method // of Method Class. import java.lang.reflect.Method; // This is Main Class public class GFG { // main function 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 Comment More infoAdvertise with us Next Article Set equals() Method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Method Class java-lang-reflect-package +1 More Practice Tags : Java Similar Reads Set equals() Method in Java In Java, the equals() method of the Set class is used to compare two sets for equality. It checks if two sets contain the same elements regardless of their order.Example 1: This example demonstrates how to compare two HashSet collections for equality of type string using the equals() method.Java// J 2 min read Vector equals() Method in Java The java.util.vector.equals(Object obj) method of Vector class in Java is used verify the equality of an Object with a vector and compare them. The list returns true only if both Vector contains same elements with same order. Syntax: first_vector.equals(second_vector) Parameters: This method accepts 2 min read MathContext equals() Method in Java The equals() method in the Java MathContext class is a part of the java.math package. This method is used to compare one MathContext object with another and checks whether both objects have the same precision and rounding mode settings.This method is very useful when we want to verify if two MathCon 2 min read YearMonth equals() method in Java The equals() method of YearMonth class in Java is used to compare two YearMonth objects. It compares this YearMonth object to the YearMonth object passed to it as parameter and checks if two YearMonth instances are equal or not. Syntax: public boolean equals(Object otherYearMonth) Parameter: This me 2 min read Java File Class equals() Method with Examples The equals() method of Java File Class compares the pathname supplied in the argument to the pathname provided in the argument. If the parameter is not null and points to the same file or directory, this function returns true. The operating system determines if the two abstract pathnames are equival 2 min read Charset equals() method in Java with Examples The equals() method is a built-in method of the java.nio.charset checks if a given object of charset is equal to another given object of the charset. Two charsets are considered equal if, and only if, they have the same canonical names. A charset is never equal to any other type of object. Syntax: p 2 min read Like