The code below is the instantiation of an object which is implemented with an anonymous class.
what I am not so clear on is the exact life span of the object created by the anonymous class.
the line
PictureCallback jpegCallback;
creates a variable of the PictureCallback type and assigns the name jpegCallback to it. This variable is a member variable of the MainClass class.
Next it instantiates a new object of PictureCallback type and stores that into the jpegCallback variable.
what i don't understand is exactly when is this code executed. Because the PictureCallback jpegCallback variable is a member of MainClass it looks like it must be created when the MainClass is loaded by the class loader, at the same time as it's other member variables and static variables of MainClass.
so if this is true then the PictureCallback anonymous class must be created in the same way as a static inner class or a static variable. at the beginning of runtime.
the anonymous object must exist like a static because if it stopped existing during the middle of the runtime then if the callback method was called the action defined in the overridden method could not occur, because the object no longer exists.
my best guess is that this object is created like a static object as the MainClass is loaded and it continues until the MainClass is destroyed.
can someone explain when this anonymous class object exists, and when it is cleaned out by the garbage collector or is no longer accessible?
public class MainClass extends Activity {
PictureCallback jpegCallback = new PictureCallback() {
@Override void onPictureTaken(byte[], data, Camera camera){
// some action performed
}
}
} // end of MainClass
what i don't understand is exactly when is this code executed
Whenever an instance of MainClass
is executed. Note that each instance of MainClass
which is created will in turn create a new instance of the anonymous class.
Because the PictureCallback jpegCallback variable is a member of MainClass it looks like it must be created when the MainClass is loaded by the class loader, at the same time as it's other member variables and static variables of MainClass.
No, not at all. It's not a static variable, so it isn't initialized when the class is initialized - it's only initialized when an instance is initialized.
so if this is true then the PictureCallback anonymous class must be created in the same way as a static inner class or a static variable. at the beginning of runtime.
I'd expect the class itself to be initialized when the first instance of MainClass
is created.
my best guess is that this object is created like a static object as the MainClass is loaded and it continues until the MainClass is destroyed.
No. It's handled like any other instance variable. Assuming there are no other references to the object, it will be eligible for garbage collection when the MainClass
instance is eligible for garbage collection.
You can think of your code as being like this:
public class MainClass extends Activity {
PictureCallback jpegCallback;
public MainClass() {
jpegCallback = new PictureCallback() {
@Override void onPictureTaken(byte[], data, Camera camera) {
// some action performed
}
};
}
}
Does that help? Don't forget that as far as the runtime is concerned, the anonymous class is just a class which extends PictureCallback
and has a reference to an instance of MainClass
. It doesn't really care that it was synthesized by the compiler.
See more on this question at Stackoverflow