Close

Java Reflection - Field.getDeclaringClass() Examples

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 Class<?> getDeclaringClass()

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);
}

private static interface MyInterface {
int anInt = 3;
}
}

Output

interface com.logicbig.example.field.GetDeclaringClassExample2$MyInterface




See Also