πŸŽ‰ Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
β†’ Click Here ←

Member interface in Reflection API  


Introduction
  • Member interface is part of the Reflection API and represents a single member (Field, Method or Constructor) of a class.
  • It is present in the java.lang.reflect package.
  • It provides methods to get metadata about the member such as its name, declaring class and modifiers.
  • The classes that implement Member interface are:
    • Field – represents class fields (variables)
    • Method – represents class methods
    • Constructor – represents class constructors
  • Member Interface in Java Reflection
  • Note: Class class does not implement Member interface because it represents the class itself, not a member inside it.

Important Methods of Member Interface
  • Some important methods of Member interface are as follows :-
    S.No Method Use
    1 getName() Returns the name of the member (field/method/constructor).
    2 getDeclaringClass() Returns the Class object representing the class where this member is declared.
    3 getModifiers() Returns the Java language modifiers (public, private, static, etc.) as an int.
    4 isSynthetic() Checks if the member is a compiler-generated synthetic member.
  • Program:
    import java.lang.reflect.*;
    
    class Student
    {
        public String name;
        private int age;
    
        public Student() {}
        public void study() {}
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Class<?> clazz = Student.class;
    
            // Get declared fields and print Member details
            for (Field field : clazz.getDeclaredFields())
            {
                System.out.println("\nMember Name: " + field.getName());
                System.out.println("Declaring Class: " + field.getDeclaringClass().getSimpleName());
                System.out.println("Modifiers: " + Modifier.toString(field.getModifiers()));
                System.out.println("Is Synthetic: " + field.isSynthetic());
            }
    
            // Get declared methods and print Member details
            for (Method method : clazz.getDeclaredMethods())
            {
                System.out.println("\nMember Name: " + method.getName());
                System.out.println("Declaring Class: " + method.getDeclaringClass().getSimpleName());
                System.out.println("Modifiers: " + Modifier.toString(method.getModifiers()));
                System.out.println("Is Synthetic: " + method.isSynthetic());
            }
    
            // Get declared constructors and print Member details
            for (Constructor<?> constructor : clazz.getDeclaredConstructors())
            {
                System.out.println("\nMember Name: " + constructor.getName());
                System.out.println("Declaring Class: " + constructor.getDeclaringClass().getSimpleName());
                System.out.println("Modifiers: " + Modifier.toString(constructor.getModifiers()));
                System.out.println("Is Synthetic: " + constructor.isSynthetic());
            }
        }
    }