java.lang.reflect.Method.equals() Method Example



Description

The java.lang.reflect.Method.equals(Object obj) method compares this Method against the specified object. Returns true if the objects are the same. Two Method objects are the same if they were declared by the same class and have the same formal parameter types.

Declaration

Following is the declaration for java.lang.reflect.Method.equals(Object obj) method.

public boolean equals(Object obj)

Parameters

obj − the object to compare.

Returns

true if this object is the same as the obj argument; false otherwise.

Example

The following example shows the usage of java.lang.reflect.Method.equals(Object obj) method.

package com.tutorialspoint;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

public class MethodDemo {

   public static void main(String[] args) {

      Method[] methods = SampleClass.class.getMethods();
      boolean isEquals = methods[0].equals(methods[1]);
      System.out.println("Methods are " + (isEquals?"equal.":"not equal."));
   }
}

class SampleClass {
   private String sampleField;
   private String sampleField1;

   public SampleClass(){
   }

   public SampleClass(String sampleField){
      this.sampleField = sampleField;
   }

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;
   }

   public String getSampleField1() {
      return sampleField1;
   }

   public void setSampleField1(String sampleField1) {
      this.sampleField1 = sampleField1;
   } 
}

Let us compile and run the above program, this will produce the following result −

Methods are not equal.
java_reflect_method.htm
Advertisements