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?
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
According to official developers guide -
This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With