Open In App

Field getDeclaringClass() method in Java with Examples

Last Updated : 26 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The getDeclaringClass() method of java.lang.reflect.Field is used to get the Class object which declares the field represented by this Field object. If Field object is present and we want to get the class object then we can get that class object using this method.

Syntax:

public Class<T> getDeclaringClass()

Parameters: This method accepts nothing.

Return value: This method returns an object representing the declaring class of the underlying member.

Below programs illustrate getDeclaringClass() method:
Program 1:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // Get the value field object
        Field field
            = User.class
                  .getField("identificationChar");
  
        // get the declaring class object
        Class declaringClass
            = field.getDeclaringClass();
  
        // print result
        System.out.println("Declaring Class"
                           + " for Field Object: "
                           + declaringClass);
    }
}
  
// sample User class
class User {
  
    // static char values
    public static char identificationChar = 'E';
    public static char selectionChar = 'A';
    public static String name = "Aman";
  
    // getter and setter methods
    public static char getIdentificationChar()
    {
        return identificationChar;
    }
  
    public static void
    setIdentificationChar(char identificationChar)
    {
        User.identificationChar = identificationChar;
    }
  
    public static char getSelectionChar()
    {
        return selectionChar;
    }
  
    public static void
    setSelectionChar(char selectionChar)
    {
        User.selectionChar = selectionChar;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        User.name = name;
    }
}


Output:

Declaring Class for Field Object: class User

Program 2:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // Get the value field object
        Field field
            = Alphabets.class.getField("value");
  
        // get the declaring class object
        Class declaringClass
            = field.getDeclaringClass();
  
        // print result
        System.out.println("Declaring Class: "
                           + declaringClass);
    }
  
    // Alphabets class
    static class Alphabets {
  
        // char field
        public static char value = 'Q';
  
        // getter and setter methods
        public static char getValue()
        {
            return value;
        }
  
        public static void setValue(char value)
        {
            Alphabets.value = value;
        }
    }
}


Output:

Declaring Class: class GFG$Alphabets

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getDeclaringClass–



Similar Reads

Constructor getDeclaringClass() method in Java with Examples
The constructor class provides information about a single constructor for a class and it also provides access to that constructor. The getDeclaringClass() method of java.lang.reflect.Constructor is used to return the class object representing the class that declares the constructor represented by this object. This method returns the name of the sou
2 min read
Class getDeclaringClass() method in Java with Examples
The getDeclaringClass() method of java.lang.Class class is used to get the declaring class of this class. The method returns the declaring class of this class, if this class or interface is a member of another class. Else this method returns null. Syntax: public Constructor getDeclaringClass() Parameter: This method does not accept any parameter. R
2 min read
Method Class | getDeclaringClass() method in Java
The java.lang.reflect.Method.getDeclaringClass() method of Method class returns the Class object for the class or interface which defines the method on which we are applying this getDeclaringClass() method. Syntax: public Class&lt;?&gt; getDeclaringClass() Return Value: This method returns the Class object in which method is declared. Below program
4 min read
Field getAnnotatedType() method in Java With Examples
The getAnnotatedType() method of java.lang.reflect.Field is used to return an annonatedType object that represents the use of a type to specify the declared type of the field. Every Field of the class is declared by some AnnotatedType.AnnotatedType represents the potentially annotated use of any type including an array type, a parameterized type, a
2 min read
Field getBoolean() method in Java with Examples
The getBoolean() method of java.lang.reflect.Field class is used to get the value of a static or instance Boolean field contains in a class. When a class contains a static or instance Boolean field and we want to get the value of that field then we can use this method to return the value of Field. Syntax: public boolean getBoolean(Object obj) throw
3 min read
Field equals() method in Java with Examples
The equals() method of java.lang.reflect.Field is used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the same name and type. This method is very helpful at th
3 min read
Field getChar() method in Java with Examples
The getChar() method of java.lang.reflect.Field is used to get the value of char which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type char via a widening conversion. When a class contains a static or instance Char field and we want to get the value of that field then we
3 min read
Field getFloat() method in Java with Examples
The getFloat() method of java.lang.reflect.Field used to get the value of float which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type float via a widening conversion. When a class contains a static or instance float field and we want to get the value of that field then we
3 min read
Field getDouble() method in Java with Examples
The getDouble() method of java.lang.reflect.Field used to get the value of double which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type double via a widening conversion. When a class contains a static or instance double field and we want to get the value of that field the
3 min read
Field getByte() method in Java with Examples
The getByte() method of java.lang.reflect.Field used to get the value of byte which has to be static or instance field type. When a class contains a static or instance byte field and we want to get the value of that field then we can use this method to return the value of Field. Syntax: public Byte getByte(Object obj) throws IllegalArgumentExceptio
3 min read