|
|
| java |
Expert:ravurivinodkumar
What is Externalization and how it can be implement in our application? Can any one please explain me with some examples?
Thanks in advance Vinod
|
| Answers |
Hi friend...
Externalizable interface :
When user use Serializable interface, class is serialized by default but Externalizable interface provides facility to complete control over serialization process so Externalization is a more advanced alternative to serialization. This interface has two methods: 1:- public void writeExternal(ObjectOutput out) 2:- public void readExternal(ObjectInput in)
Every class that usese/implements externalization must implement Externalizable interface and override these methods and must have a no arguments constructor. These methods are automatically called at the time of serialization and de-serialization so you can can implement your own serialization-specific operations and have control over what data to serialize and wat to not.
Example Code :
import java.io.*;
public class MyExternObject implements Externalizable {
int i; String s;
MyExternObject() { i = 256; s = new String("Implementation of interface Externalizable."); }
public int getI() { return i; }
public String getS() { return s; }
public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.i); out.writeObject(this.s); }
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.i = in.readInt(); this.s = (String) in.readObject(); }
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyExternObject externObject = new MyExternObject(); System.out.println(externObject.getS()); System.out.println(externObject.getI()); } }
Output : Implementation of interface Externalizable. 256
Thanks..
|
Read for more information,
http://www.roseindia.net/java/
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|