Jezer Crespo Jezer Crespo - 4 months ago 15
Android Question

".class" keyword in Java

I'm currently learning to program on Android using the book Android Programming: The Big Nerd Ranch Guide, and I've encountered this line of code

Intent i = new Intent(getActivity(),CrimeActivity.class);


I can't seem to understand why the Intent's constructor need that second argument.
If my knowledge serves me right, classes in Java are only a blueprint of an object..

So I'm confused why a literal class is passed as an argument in the Intents constructor?

What's happening right there?

Answer

In Java, everything except primitive types, is an object. The class definitions we write are wrapped in an Object of Class class. For instance:

class Foo{
}

Foo.class is an instance of Class class.

Class objects hold the information about the class information, like: name, list of instance variables, list of methods etc.

This information can be used at runtime via reflection.

Documentation