• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Externalizable interface

 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great link, Map. Thanks.
The Object Serialization Section of Sun's Java Tutorial is definitely worth reading, also.
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the foll code which i used to try Externalizable interface throws an eception as below on execution

the exception is
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code seems to compile and run as expected when the aforementioned mistake is corrected.
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend that you take a look at the documentation and the source code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic