Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does implementing Externalizable need a default public constructor?

We don't need it if we're implementing Serializable. So why this difference? How does it relate to the actual mechanism of Serialization?

like image 880
shrini1000 Avatar asked Dec 25 '11 02:12

shrini1000


People also ask

What is purpose of Externalizable interface?

Interface Externalizable The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state.

Which of the following is best case to use the Externalizable interface in?

We can achieve custom serialization with the Serializable interface by marking the field with transient keyword. The JVM won't serialize the particular field but it'll add up the field to file storage with the default value. That's why it's a good practice to use Externalizable in case of custom serialization.

What is difference between Serializable and Externalizable in Java?

Externalization provides implementation logic control to the application by overriding readExternal and writeExternal methods. In serializable interface uses reflection which causes relatively slow performance. Externalizable gives full control over the implementation approach.

Why do we need externalization in Java?

Externalization in Java is used to customize the serialization mechanism. Java serialization is not much efficient. When we have bloated objects that hold several attributes and properties, it is not good to serialize them. Here, the externalization will be more efficient.


2 Answers

A thorough explanation (although the grammar of the article might be improved) can be found on http://www.jusfortechies.com/java/core-java/externalization.php . The short answer, for future reference in case the linked page goes away:

Externalizable is an interface extending Serializable. Contrary to Serializable, though, objects are not restored by just reading the serialized bytestream, but the public constructor is called and only once the object is thus created, its state is restored. This makes restoring more efficient.

Edit: See also What is the difference between Serializable and Externalizable in Java? .

like image 142
jstarek Avatar answered Nov 03 '22 00:11

jstarek


This is primarily used for caching purposes. In order to deserialize across streams, you will need to spell out how you want your object to be deserialized, hence the two methods provided by the contract in Externalizable interface: writeExternal and readExternal. Note that Externalizable extends Serializable, so you don't necessarily need to implement Serializable interface (although it's a marker interface and there are no methods to be actually implemented).

For a sample implementation, have a look at MimeType.

like image 36
S.R.I Avatar answered Nov 03 '22 00:11

S.R.I