The “.class” keyword in Java
I’m currently using this book to learn to program on Android Programming: The Big Nerd Ranch Guide, which I came across before
Intent i = new Intent(getActivity(),CrimeActivity.class);
I can’t seem to understand why the constructor of the intent requires a second parameter.
If my knowledge is useful to me, a class in Java is just a blueprint for an object.
So I’m confused why the literal class is passed as a parameter in the Intents constructor?
What’s going on there?
Solution
In Java, everything except the basic type is an object. The class definition we write is wrapped in a class object. For example:
class Foo{
}
Foo.class
is an instance of the Class class.
Class objects hold information about the class, such as name, list of instance variables, list of methods, and so on.
This information is available at runtime via reflection .