my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Externalizable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Externalizable interface" Watch "Externalizable interface" New topic
Author

Externalizable interface

Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
can somebody xplain how to use the externalizable interface to write my own serialization.
i would appreciate an example that demonstrates writing and reading of an object which implements the Externalizable interface.
thanx in advance...
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
"Component Development for the Java Platform" by Stuart Dabbs Halloway (chapter4) has an excellent treatment on serialization (in particular, there are good explanations of potential problems with externalizable You can download the book here.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Great link, Map. Thanks.
The Object Serialization Section of Sun's Java Tutorial is definitely worth reading, also.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
the foll code which i used to try Externalizable interface throws an eception as below on execution

the exception is
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I haven't really looked at your code or tried to figure out the problem, but one potential source of confusion/bugs is your exception handling strategy in the writeExternal and readExternal methods. If an exception were thrown during the execution of either method, nobody would ever know about it - they just bury the exception and return. I'd at least print a message to the error output stream, System.err .
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Otherwise, a quick look at the documentation for InvalidClassException tells us under what circumstances this exception is thrown:
Thrown when the Serialization runtime detects one of the following problems with a Class... The class does not have an accessible no-arg constructor.
Does your class have an accessible no-arg constructor? Not if the client trying to access it is not in the same package.
Getting any ideas?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Your code seems to compile and run as expected when the aforementioned mistake is corrected.
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
thanx dirk. it helped.
so it actually is cos whatever class is trying to create the object of my test class is in the default jdk package!
But any ideas as to how the instantiation is actually done. i.e. which class handles it.
thanx
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I recommend that you take a look at the documentation and the source code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Externalizable interface
 
Similar Threads
Serializable and Externalizable Interface
Externalizable interface
Externalizable
Serializable Vs Externalizable
Externalizable Vrs Serializable