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.
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.
Related
I am beginner in programming and i just started working with greenfoot (software for creating games using Java). When i was writing code i had to use construction builded in greenfoot which was using parameter described as: java.lang.class where i had to type ClassName.class . I was trying to go through documentation and a lot of other sources to figure out how it works and what is this, but to be honest i couldnt understand anything. Is there is someone who can tell me in simply words what does it mean and how construction like ClassName.class works? That is the first time i see dot notation used like this, before I have seen only dot notation when i tried to refer to for example other class method like: OtherClass.method() . So is it just builded in instance variable of every class or something like this? Thanks in advance
It's called a class literal. ClassName.class provides access to the instance of Class that represents the given class. So for instance, String.class refers to the Class instance for the String class. Using that object, you can do do various things (like find out what its members are at runtime via reflection). (FWIW, objects provide also access to the Class instance for their class via their getClass method.)
I need to have an app that during run-time can load another class and deserialize one of it's instance successfuly so that it can run it's methods and read the member variables.
From what I noticed, you can not serialize methods in Java, so I'm thinking about serializing the class instance in project2, load the compiled class with ClassLoader in project1, instantiate it and assign the deserialized instance from porject2 in project1 to the instantiated loaded class.
The serialized class will inherit the same parent class in both projects.
Is this the best way to go? It's a school project so the requirements are that my app can accept any other type of class without changing the code.
TL;DR: My plan is to load a compiled class with the ClassLoader so that my project knows about that class (specifically the methods inside) and then load that serialized class instance inside the project so that I can get the data from the instance and together with the loaded class (now I know the methods aswell), run the methods on the deserialized instance.
You are mistaken. The ability to call a method on some object isn't related to serialization at all.
What I mean: the method implementation is not part of the serialized data! Java serialization only writes field data into that output stream.
The implementation of a method only depends on the class file of some Java class. You can serialize and deserialize your objects as often as you want to - but what happens when you call a method on such an object is only determined by the class file that the corresponding class loader loaded for you when first accessing the corresponding class.
If your goal is really just about "one class dumps an object into a binary representation"; and another piece of code loads that binary data; turns it into an object; to access that object; then you do not need two projects. You also do not need to worry about "the methods being" there. As long as your ClassLoader knows the class of objects to be de-serialized, everything will just work. Just pick an example tutorial, like this here and work through it.
But: when your requirement is to invoke methods or access fields of arbitrary objects; then you don't look into serialization, but into Java reflection.
But a word of warning there: reflections sounds easy, but be assured: there are many many ways for you to write slightly wrong code. And because reflection is basically a runtime thing, the java compiler doesn't help much. You write code that looks reasonable, it compiles, you run it, and you get exceptions throw at you.
In that sense, reflection is an advanced topic in the Java curriculum; and I think you should rather step back and clarify with your teachers what exactly they expect from you.
Given your latest updates: then simply look into that tutorial about serialization (and forget about the reflection part). And to answer your question: yes, that sounds like a viable approach. Can't say more; as you are not sharing code so far.
Times ago, I was looking for a way to start ServiceMode programmatically. The great obstacle was how to understand the parameters that the class must receive (in its case, it is the SECRET CODES). And off course, the device must be rooted.
It had demanded plenty of research but I finally figured out how to start a system activity through command line. Now I want to create a class that extends from it. However, I don't have its API reference because it is hidded in Android internals and only accessible through things like reflection. I have only its name: com.sec.android.app.servicemodeapp.ServiceModeApp.
So, I'm wondering if Android/Java API let me create a class declaration in the form of:
public class MyCustomSystemActivity extends Class.forName("com.sec.android.app.servicemodeapp.ServiceModeApp") { ... }
Of course, this sentence is not allowed.
Can someone give me a hint if it is possible in other ways? And if yes, how can I declare such thing?
No, this is not possible. In a compiled language such as Java, the compiler needs to know what members are inherited and what methods are overridden, and it can't do this if it doesn't know what class is being extended.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In Java, what does a reference to Class.class do?
Firstly apologies for asking this question here. I know it's a simple question and can probably be answered quite quickly and easily. However searching for Java .class is such a loaded search term it brings up nothing useful.
Having said that, I just started learning Java and I just came across something like this:
Intent i = new Intent(this, ReminderManageActivity.class);
I am not sure, but I think ReminderManageActivity.class is an attribute that gives a string of the name of the class? Is that correct?
I know it sounds stupid, but class is a Class object that represent the Class.
If you want to instantiate / use some class, and you want to get this class as parametr, you can't just write Intent i = new Intent(this, ReminderManageActivity); since ReminderManageActivity is not an object.
In Java, given an object, an instance of a class, you can get the class name by coding the following:
Class clazz = obj.getClass();
String clazzName = clazz.getName();
Sometimes you want you want to create a Class object for a given class. In this case you can do so by writing code similar to the following example:
Class clazz = MyClass.class;
source
If you just started learning java than you will see more about this when you will study Type Information or RTTI(Runtime Type Information)
ReminderManageActivity.class
is called a class literal and it produces a reference to the Class object, which contains information about the class in question.
It is similar to the static method :
Class.forName("ReminderManageActivity");
It is an instance of java.lang.Class which is part of Java Refleciton API. You can get more informaiton about the API here
Because everything in Java is an object, so even classes can be represented by objects.
ReminderManageActivity.class returns a Class object representing the ReminderManageActivity class.
A Class object contains information of a class, such as class' name, class' method, class's variables etc. You can look at the class API for more information: http://download.oracle.com/javase/6/docs/api/java/lang/Class.html
I'm building a small Android application, but this is more of a Java question than an android question. Looking through the tutorials there are lines that look like:
startService(new Intent(this, MyService.class));
what exactly does the "MyService.class" field represent? Is that just a reference to the class for a template?
Thanks.
Andy's answer is definitely correct, but I want to expand on the point a little.
.class is a special syntax for obtaining an instance of a Class object. It can be used when only the type is available and no instance of the related object is around. It can be used with any concrete type name, including arrays and primitives. For instance, int.class is valid.
This way (and other ways) to get a Class object are documented in the old Sun reflection API docs.
The special .class syntax often appears in idiomatic usage as a "type token" in generic code. A class literal obtained with .class is called a type token when "passed among methods to communicate both compile-time and runtime type information" (Joshua Bloch's Effective Java 2e, p. 142).
Yes, MyService.class returns a Class object that represents the class MyService. The Intent uses it to identify which Service or Activity you're intending to start.
The MyService.class allows you to get the Class object describing the MyClass class, from the class name alone (as opposed to have an instance of the class to ask for object.getClass()).
In JVM, when a class is loaded, an object of the type Class represents the loaded class in memory. com.abc.MyClass.class is a literal expression that represents the Class object for the class com.abc.MyClass.
The same Class object can also be obtained by calling myClassReference.getClass() method if you have a reference to an object of the class.
The Class object can be used to find the information on the structure of the class, access fields, invoke methods and instantiate objects of the class using Java Reflection API.