Explain Serialization
Serialization allows you to create a JVM-independent binary representation of an in-memory Java Object. This external representation may be used to tranfer (or) store the object and to recreate it in another JVM.
static variables are NOT part of serialized object.
Object Serialization is interesting because it allows you to implement lightweight persistence.
You might wonder what’s necessary for an object to be recovered from its serialized state. For example, suppose you serialize an object and send it as a file or through a network to another machine. Could a program on the other machine reconstruct the object using only the contents of the file?
NO that's NOT sufficient. You’ll get a ClassNotFoundException. The JVM must be able to find the associated .class file.
Object serialization was added to the language to support two major features. Java’s Remote Method Invocation (RMI) allows objects that live on other machines to behave as if they live on your machine. When sending messages to remote objects, object serialization is necessary to transport the arguments and return values.
Object serialization is also necessary for JavaBeans, described in Chapter 14. When a Bean is used, its state information is generally configured at design time. This state information must be stored and later recovered when the program is started; object serialization performs this task.
A particularly clever aspect of object serialization is that it not only saves an image of your object, but it also follows all the references contained in your object and saves those objects, and follows all the references in each of those objects, etc. This is sometimes referred to as the “web of objects” that a single object can be connected to, and it includes arrays of references to objects as well as member objects. If you had to maintain your own object serialization scheme, maintaining the code to follow all these links would be a bit mind-boggling. However, Java object
serialization seems to pull it off flawlessly, no doubt using an optimized algorithm that traverses the web of objects.
Bruce Eckel - Thinking in Java - Object serialization
Return Statement in Finally Block
import java.io.IOException;
public class FinallyCheck {
int display() {
int i = 10;
try {
throw new IOException("IOException throwed ");
}catch(IOException e) {
i = 40;
return i;
} finally {
i = 60;
System.out.println("Inside finally block");
// return i; // o/p ==> 60
}
}
public static void main(String[] args) {
System.out.println(new FinallyCheck().display());
}
}
/*
Output:
Inside finally block
40
This is because u return the value of i in the catch block. even though u assign a new value to i in the finally block that value is not returned.
incase u want the value set in finally to be visible , return i in the finally blk i.e.,
} finally {
i = 60;
return i; // o/p ==> 60
}
*/


