Jezer Crespo
Jezer Crespo

Reputation: 2172

".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?

Upvotes: 4

Views: 4047

Answers (3)

rocketboy
rocketboy

Reputation: 9741

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

Upvotes: 7

Ridcully
Ridcully

Reputation: 23655

You're right, that the class is something like a blueprint for an object. You give the Intent you create that "blueprint" because the Intent itself (resp. the Android system when finally serving your Intent) will create an instance (an object) of the class you passed to it.

That's the reason you pass just the class and not an instance to an Intent.

Upvotes: 1

mihirjoshi
mihirjoshi

Reputation: 12201

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.

Upvotes: 2

Related Questions