Correct meaning of SecondActivity.class in intents in Android?

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startService(intent);

      

I figured until this line of code "Object SecondActivity" is created and we send Runtime data to SecondActivity to Intent. So I think at runtime the compiler is adding extra code to the second activity.

I know it has to do with Reflection, but still I am not getting it right. "Class" denotes "static variable class" of type "class" in class "SecondActivity" at runtime, something like

static Class class = .......

      

Are we accessing a class object through a "static class variable"? Does the compiler add this static variable at runtime through which we get an object of the "SecondActivity" class? Do I understand correctly?

+3


source to share


2 answers


If I understand your question, it is "what is SecondActivity.class?" and / or "what is a class?"

While not necessarily an Android question, I understand the context and agree that it can be interpreted as such.

Essentially, ".class" refers to a class that (in your example) is an Android service application, so the call startService(intent)

will instantiate an object from that class and set it up as a service.

A valid SecondActivity.class

one can be thought of as a permanent reference to the "SecondActivity" class. All of this is driven by the Java Virtual Machine (JVM).



This means that calling it SecondActivity.class

as a static variable is almost right, except that it is more of a static constant, since it will always refer to a named class SecondActivity

, which will either be or will be loaded into memory.

Extended explanation:

The class reference will cause the current ClassLoader to be called to get or load the specified class. If the class has not been loaded, it is created and the code block is called static{}

. Only one instance of this class can exist in the ClassLoader, and depending on which ClassLoader you are using, it might decide to check if the parent's ClassLoaders are already loaded.

0


source


Write it down as



Intent intent = new Intent(YourActivity.this, SecondActivity.class);
startService(intent);
      

Run codeHide result


This means that you are referring to a second activity that has not yet been created, so you are using your class file to load the class into memory

-3


source







All Articles