Reputation: 1469
As question said what JVM gives extra treatment to classes implementing Marker interface . For example I have test it using Serializable like below:
import java.io.*;
public class SerialazationDemo {
public static void main(String[] args) {
//serialized object
/* Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try {
FileOutputStream fileOut = new FileOutputStream("E:\\temp\\employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
} catch (IOException i) {
i.printStackTrace();
}*/
//deserialized object
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("E:\\temp\\employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
class Employee {
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + address);
}
}
And i found that jvm gives exception as java.io.NotSerializableException But FILES was created on given path, similar exception for deserialization. So why JVM required it to be serialized , It could directly allow creating a serialization.?
Upvotes: 0
Views: 1222
Reputation: 1867
What is a Marker Interface?
An interface is called a marker interface when it is provided as a handle by java interpreter to mark a class so that it can provide special behaviour to it at runtime and they do not have any method declarations
Marker interface just marks a class as being of a particular type.
Why marker Interface is required?
Marker interface is used as a tag to inform a message to the java compiler so that it can add special behaviour to the class implementing it.So if JVM sees a Class is Serializable it done some special operation on it
What JVM does with Serialiize Interface?
When a class implements serializable interface it assures JVM that this class may provide a special behavior(Serialization/Deserialization) at run time.
So when you are trying to serialize object JVM wants to confirm that whether class of object of this type has made a contract(Implementing Serialize interface) to show this special behavior at runtime(To be serialize/deserialized).If it has implemented,JVM is assured that it can proceed further for serialization Otherwise it throws exception that you are trying to serialize an object that does't agree to terms and condition of being ready for serialization or deserialization.
Upvotes: 0
Reputation: 310903
question said what JVM gives extra treatment to classes implementing Marker interface
The correct answer to that question is nothing. But it probably requires eludication, as the questioner may be using terms imprecisely.
The JRE treats them specially in appropriate places. This action is located in the Java Class Library, not in the JVM. For example:
Object.clone()
tests that the object implements Cloneable.
ObjectOutputStream.writeObject()
tests that the object being written implements Serializable.
rmic
and parts of the RMI runtime test that a remote object implements a remote interface, which means an interface that extends Remote.
This has nothing to do with the JVM per se.
I found that jvm gives exception as java.io.NotSerializableException
No you didn't. You found that ObjectOutputStream.writeObject()
threw an exception. It isn't the same thing.
But FILES was created on given path
The file was created by new FileOutputStream(...),
which had already executed before you got the exception.
So why JVM required it to be serialized , It could directly allow creating a serialization.?
It doesn't. See above. But ObjectOutputStream.writeObject()
does, and it does so because making everything Serializable
has numerous disadvantages that need to be considered before you suggest it. Consider for example the security risk of a serializable password field that you didn't intend.
Upvotes: 4
Reputation: 68715
If the object is not Serializable
its state cannot be saved using ObjectOutputStrem
.
File must be created because that is done using FileInputStream
and as soon as you close the stream, there will be a new file. But if try to check the contents of the file, it should be empty, because the state of non-serializable state will not be saved.
Upvotes: 0
Reputation: 223013
The reason you're required to extend Serializable
(or Externalizable
) is to ensure that you're only serialising classes that are designed for serialisation. You certainly don't want to serialise classes that aren't designed for it, because then their format would be quite fragile; the smallest change to the class would break your deserialisation.
One could argue that the same fragility applies to classes marked Serializable
, but you're supposed to think about serial form stability when marking a class as Serializable
(or else explicitly disavow any stability, as Guava does).
Upvotes: 0