Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 659
Jezer Crespo Avatar asked Feb 16 '23 09:02

Jezer Crespo


2 Answers

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

like image 190
rocketboy Avatar answered Feb 17 '23 22:02

rocketboy


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.

like image 38
mihirjoshi Avatar answered Feb 17 '23 22:02

mihirjoshi