Quick revision
The short answer
The Reflection API lets code inspect classes at run time and use their members without knowing them at compile time: read and write fields, invoke methods and create instances. Its core classes are Class plus Field, Method, Constructor, Modifier, Parameter and RecordComponent in java.lang.reflect. getMethods() returns all public methods, including inherited ones from superclasses and interfaces, while getDeclaredMethods() returns every method declared in that class itself, whatever its access, but none inherited.
What to remember
In this answer, we saw that reflection lets code inspect classes at run time, read and write fields, invoke methods and create instances, using Class together with Field, Method, Constructor, Parameter, RecordComponent and Modifier. getMethods() returns public methods including inherited ones, while getDeclaredMethods() returns every method declared in the class itself but none inherited, and non-public members need setAccessible(true), which strong encapsulation blocks for packages that are not opened to us.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The Reflection API lets code inspect classes at run time and use their members without knowing them at compile time: read and write fields, invoke methods and create instances. Its core classes are Class plus Field, Method, Constructor, Modifier, Parameter and RecordComponent in java.lang.reflect. getMethods() returns all public methods, including inherited ones from superclasses and interfaces, while getDeclaredMethods() returns every method declared in that class itself, whatever its access, but none inherited.
Why this question is asked
Most of us rarely call reflection directly, yet almost every framework we use is built on it: Spring finds @Autowired fields, Jackson reads getters, JUnit finds @Test methods, and Hibernate fills entity fields. Knowing the API explains how those frameworks work and why they sometimes fail on newer JDKs. Interviewers ask the getMethods versus getDeclaredMethods question because the difference is easy to confuse and causes real bugs. In this answer, we will recap what reflection is, meet the core classes, compare the two methods with an example, look at access to private members, and find a private method in a superclass.
1A Quick Recap of Reflection
Normally the compiler knows every method we call. When we write dog.bark(), the compiler checks that Dog has a bark() method, and the call is fixed in the bytecode. Reflection is the opposite: the program asks at run time what a class contains, and then uses those members by name. The starting point is a Class object, which represents a loaded class. We get one with Dog.class, with dog.getClass() on an instance, or with Class.forName("com.acme.Dog") from a name.
From the Class object we reach the members, each represented by its own class in java.lang.reflect. Field represents a field and can read or write its value. Method can be invoked. Constructor creates new instances. Parameter describes a method or constructor parameter, RecordComponent describes a component of a record, and Modifier is a helper that decodes the int flags such as public, static and final.
| Class | Represents | Typical use |
|---|---|---|
| Class | A loaded class or interface | Entry point: list members, check type |
| Field | A field | get(obj), set(obj, value) |
| Method | A method | invoke(target, args) |
| Constructor | A constructor | newInstance(args) |
| Parameter | A method or constructor parameter | getName(), getAnnotations() |
RecordComponent | A record component | getAccessor() to read the value |
| Modifier | Helper for modifier flags | Modifier.isStatic(m.getModifiers()) |
2Using Reflection in a Few Lines
Here is what the API looks like in practice. We look up a class by name, create an instance, call a method and change a field, without the compiler knowing any of these members.
Notice that every name is a string, so a typo becomes a NoSuchMethodException or NoSuchFieldException at run time instead of a compile error. Notice also the setAccessible(true) call before touching the private field. That is how frameworks reach private members, and it is the part that newer JDKs restrict, as we will see shortly.
3GetMethods() Versus getDeclaredMethods()
The two methods answer different questions. getMethods() asks what public methods can be called on this type, so it includes public methods inherited from every superclass and interface, all the way up to Object. getDeclaredMethods() asks what methods this class's own source declares, so it includes private, protected and package-private ones, but nothing inherited. The same pattern holds for fields and constructors: getFields() and getDeclaredFields(), getConstructors() and getDeclaredConstructors().
| Method | getMethods() | getDeclaredMethods() |
|---|---|---|
bark() public, in Dog | yes | yes |
wag() private, in Dog | no | yes |
eat() public, in Animal | yes | no |
sleep() protected, in Animal | no | no |
toString() public, in Object | yes | no |
So getMethods() on Dog includes eat() from Animal and toString(), equals(), hashCode() and the other public methods of Object, but it never includes sleep() or wag(), because they are not public. getDeclaredMethods() on Dog has exactly the two methods written in Dog. Notice that neither call ever returns secret() for Dog: a private method of the superclass is neither public nor declared in Dog. Two more details: the order of the returned array is not specified, so never rely on it, and getDeclaredMethods() may include synthetic bridge methods the compiler generated, which we can filter with isSynthetic() or isBridge().
4Accessing Non-Public Members and Strong Encapsulation
A Method or Field for a private member can be found with the Declared methods, but using it throws IllegalAccessException until we call setAccessible(true), which turns off the Java language access check for that object. This is called deep reflection.
Since JDK 16, and with no way to switch it off since JDK 17, the module system enforces strong encapsulation. setAccessible(true) on a private member of a class in another module throws InaccessibleObjectException unless that module opens the package to the caller. Our own classes on the class path are fine, but private fields of JDK classes such as java.util.ArrayList are not available any more. The method trySetAccessible() returns false instead of throwing, which is handy when we only want to try.
5Follow-up questions
How do you find a private method declared in a superclass?
Neither getMethods() nor getDeclaredMethods() on the subclass returns it, so we walk the class hierarchy ourselves: start at the class, call getDeclaredMethod(name, parameterTypes) inside a try block, and on NoSuchMethodException move to getSuperclass() until it returns null. Once found, setAccessible(true) lets us invoke it, as long as the class is in our own code or in a package opened to us. Spring's ReflectionUtils.findMethod does exactly this walk.
Why is reflection slower than a direct call?
A direct call is linked once and the JIT can inline it. A reflective call must check access, box primitive arguments into an Object array, and unwrap the result. Since JDK 18, core reflection is implemented with method handles, which made it faster than it used to be, but frameworks still cache Method objects and avoid reflection on hot paths.
Does getDeclaredFields() include fields inherited from a superclass?
No. Like getDeclaredMethods(), it only returns fields declared in that exact class. To see the whole object's state we combine getDeclaredFields() for each class while walking up with getSuperclass().
6Conclusion
In this answer, we saw that reflection lets code inspect classes at run time, read and write fields, invoke methods and create instances, using Class together with Field, Method, Constructor, Parameter, RecordComponent and Modifier. getMethods() returns public methods including inherited ones, while getDeclaredMethods() returns every method declared in the class itself but none inherited, and non-public members need setAccessible(true), which strong encapsulation blocks for packages that are not opened to us.
Open with
Reflection lets a program inspect and use classes at run time without knowing them at compile time. I can list a class's fields, methods and constructors, read and write field values, invoke methods and create instances, all by name.
Then, if they want more
The entry point is the Class object, from Foo.class, obj.getClass() or Class.forName, and the members are represented by Field, Method and Constructor in java.lang.reflect, along with Parameter, RecordComponent for records, and Modifier for decoding flags like static or private. Frameworks like Spring, Jackson, JUnit and Hibernate are built on this.
The difference between the two methods is about scope and access. getMethods returns only public methods, but includes the ones inherited from superclasses and interfaces, up to Object. getDeclaredMethods returns every method declared in that class, private, protected and package-private included, but nothing inherited.
So a private method in a superclass shows up in neither on the subclass; to find it I walk up with getSuperclass and call getDeclaredMethod on each class. To use a non-public member I need setAccessible(true).
Since JDK 16 and 17, strong encapsulation means that fails with InaccessibleObjectException for packages in other modules that aren't opened, like private fields of JDK classes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Java 17, Java 18
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What can you do with the Reflection API? Name the core classes and explain the...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/a792aafe3d/