Returns the
Class
object representing the class or interface that declares the field represented by this
Field
object.
Examples
package com.logicbig.example.field;
import java.lang.reflect.Field;
public class GetDeclaringClassExample { private String aString;
public static void main(String... args) throws NoSuchFieldException { Field field = GetDeclaringClassExample.class.getDeclaredField("aString"); Class<?> declaringClass = field.getDeclaringClass(); System.out.println(declaringClass); } }
Output
class com.logicbig.example.field.GetDeclaringClassExample
Interface example:
package com.logicbig.example.field;
import java.lang.reflect.Field;
public class GetDeclaringClassExample2 {
public static void main(String... args) throws NoSuchFieldException { Field field = MyInterface.class.getDeclaredField("anInt"); Class<?> c = field.getDeclaringClass(); System.out.println(c); }