Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
We take one more sidestep during this course, this time to what's called reflection. Reflection is a programming technique that allows us to examine (and even modify) the structure of objects at run-time. During this video, we'll practice some basic reflection so that we can utilize these techniques to check for the presence of our @Doc
annotation.
Using Reflection to Interact with Methods
During this video, we take a brief look at how we can use reflection to examine a class at runtime, getting certain pieces of information about its methods. To see more, check out the official documentation of the Method class.
Using Class<?>
You may have noticed that in the video, I declared a variable with Class<?>
as follows:
Class<?> clazz = MathUtils.class;
Whenever you see a pair of angle brackets following a class name in Java, it refers to what's called a generic. Think of a generic as a parameter, except that its value must be specified as a type, and must be specified upon declaration and when using a constructor. When a class is defined using a generic (like Class
in Java is defined), upon declaration, you can specify the type of class that will pertain to the variable declaration.
Where you may have seen this before is with an ArrayList
, for example:
ArrayList<String> names = new ArrayList<String>();
In this case, you are telling the Java compiler that names
will hold an ArrayList
of String
values. Then, when you call names.get(0)
, for example, the compiler knows that the returned value is a String
, and as such you could call String
methods on it.
The question mark, as it relates to generics, is called a wildcard. Without any other keywords, the single ?
means that the type is any class that extends Object
, which is literally any Java class. You'll have limitations in what you can do with the variable itself, since the compiler won't know what type is being used. Give it a shot yourself. In workspaces, try doing the following:
import java.util.ArrayList;
public class TestString {
public static void main(String[] args) {
ArrayList<?> names = new ArrayList<String>();
names.add("Kermit the Frog");
}
}
Notice the compiler error that occurs when you try to call names.add(...)
. It's not that the ArrayList
that is created and stored into names
isn't an ArrayList
of String
s, it's just that since you used the question mark when declaring names
, the compiler doesn't know that the items will be String
s. Change the question mark to String
, and the compiler is happy.
As for the example in the video, out of brevity, I chose to use the question mark. With a few more keystrokes, I could have declared the variable as follows:
Class<MathUtils> clazz = MathUtils.class;
I chose this declaration because it has no impact on what we need to accomplish. As you can see, with an ArrayList
, it has a significant impact.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up