Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Let's start by looking at how we can access fields of a class. Java Reflection provides methods like getFields() and getDeclaredFields(). What do you think is the difference between these two methods?
I think getFields() might only return public fields.
Exactly! getFields() will only return public fields, whereas getDeclaredFields() gives you all fields, including private ones. This makes it more powerful for inspection.
So, if I want to change a private field, I should use getDeclaredFields()?
Yes! And remember, if you access a private field, you will need to set it accessible by calling field.setAccessible(true).
Can you show us an example of that?
"Sure! Here’s a code snippet:
Next, let's dive into inspecting methods. Similar to fields, we can access methods using getMethods() and getDeclaredMethods(). Can anyone remind me what the difference is?
Like fields, getMethods() only returns public methods?
That's right! getDeclaredMethods() will give you all the methods, including private. It's a crucial part of reflection for understanding class behavior.
What about actually invoking these methods?
Excellent question! After retrieving a method, you can invoke it using: method.invoke(obj, args). It's essential when working with frameworks that need dynamic method calls.
Can we see a practical example of invoking a method?
"Certainly! Here’s how you would do it:
Now that we’ve covered fields and methods let’s move on to constructors. Just like the previous inspections, you can use getConstructors() and getDeclaredConstructors(). What do you think the difference is?
I guess getConstructors() shows the public ones?
Correct! Whereas getDeclaredConstructors() retrieves all constructors of a class, regardless of their accessibility.
How do I instantiate an object using one of these constructors?
"You can do that with:
The ability to inspect and manipulate class members at runtime.
Using getFields() and getDeclaredFields() to access field data.
Utilizing getMethods() and getDeclaredMethods() for method information.
Instantiation using getConstructors() and getDeclaredConstructors().
Accessing superclass and interfaces to understand class relationships.
Reflection’s power, oh so bright, Inspecting fields and methods right!
Once upon a time, a curious coder named Alex wished to explore the hidden treasures (private fields) of a magical class (Java class). With the magic word setAccessible(true), Alex uncovered secrets beyond the reach of normal eyes!
For fields and methods, remember: FAM - Fields, Access (to private), Methods!
FAM - Fields, Access, Methods to remember what you can inspect using Reflection.
Term
Reflection
Click to flip
Term
getDeclaredFields()
Click to flip
Term
getDeclaredMethods()
Click to flip
Term
getDeclaredConstructors()
Click to flip
The ability of a Java program to analyze and manipulate its internal classes, methods, and fields dynamically at runtime.
A reflection method that returns an array of Field objects reflecting all the accessible fields declared by a class.
A reflection method that returns an array of Field objects reflecting all fields declared by a class, regardless of their accessibility.
A reflection method that returns an array of Method objects reflecting all the accessible methods declared by a class.
A reflection method that returns an array of Method objects reflecting all methods declared by a class, regardless of their accessibility.
A reflection method that returns an array of Constructor objects reflecting all the accessible constructors of a class.
A reflection method that returns an array of Constructor objects reflecting all constructors declared by a class, regardless of their visibility.
The section discusses how to inspect class members in Java using reflection, covering fields, methods, constructors, superclasses, and interfaces.
This section elaborates on inspecting class members via Java's reflection API. It details how to access fields, methods, and constructors of a class, along with the ability to explore inherited characteristics from superclasses and implemented interfaces.
In this section, we explore the powerful capabilities of Java's Reflection API to inspect various members of a class. The ability to inspect class members is essential for dynamic programming, allowing developers to retrieve metadata about fields, methods, constructors, the superclass, and interfaces at runtime.
getFields() or getDeclaredFields() to access fields of a class. While getFields() returns public fields, getDeclaredFields() retrieves all fields, irrespective of their access modifier.getMethods() or getDeclaredMethods(). Similar to fields, getMethods() lists public methods whereas getDeclaredMethods() gives a comprehensive list including private methods.getConstructors() for public constructors and getDeclaredConstructors() for all constructors.A commonly used example of inspecting methods in a class is:
for (Method m : clazz.getDeclaredMethods()) {
System.out.println(m.getName());
}This code snippet lists all the methods declared in the class represented by clazz.
In summary, inspecting class members using reflection equips developers with the tools necessary to dynamically understand and interact with classes in Java.