Close

Java Reflection - Field.equals() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

public boolean equals(Object obj)

Field class overrides Object.equals() method and compares this Field against the specified object. It returns true if the two Field objects are the same. The two objects will only be the same if they were declared by the same class and have the same name and type.


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class EqualsExample {
private int anInt;
private int otherInt;

public static void main(String... args) throws NoSuchFieldException {
Field field1 = EqualsExample.class.getDeclaredField("anInt");
Field field2 = EqualsExample.class.getDeclaredField("otherInt");
Field field3 = EqualsExample.class.getDeclaredField("anInt");

boolean equals = field1.equals(field2);
System.out.println(equals);

equals = field1.equals(field3);
System.out.println(equals);

equals = field2.equals(field3);
System.out.println(equals);
}
}

Output

false
true
false




See Also