Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

What is Serializible object?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please explain what does it mean for an Object to be Serializible and what is it for?

Thank you.
[ August 04, 2005: Message edited by: Alexander Prohorenko ]
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serializable objects are objects that ae declared to implement the Serializable interface:

e.g.

public class MySerializableClass implements Serializable {}

The serializable interface does not contain any methods. Simply declaring that a class implements the serializable interface is sufficient to meet the interface's contract.

Serializable objects can be written to streams or to disk. See:
http://java.sun.com/docs/books/tutorial/essential/io/serialization.html
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object is serializable, you can write its state outside the JVM. You might store it to a file, or send it out through a socket. Another JVM (or the same JVM at another time) can read back the state to create a clone of the original object.

Like this:


Static fields aren't written, and neither are transient ones. In fact, the "transient" keyword means "don't write this field during serialization".

Primitive data is written out pretty much the way you'd expect. Object reference fields are written the way you'd hope: the referenced object is itself serialized and writtn out, and on the other end you get the right result: two objects for the price of one, with the reference in the "top level" object pointing to the second object. And so on, recursively, if the second object contains its own references to still other objects. Watch out for NotSerializableException if some object in the hierarchy doesn't implement Serializable.

There are some low-level infrastructure details, but that should get you started. More in Chapter 9 of "Complete Java 2 Certification".

-- Phil


There's a certain amou
 
Olexandr Prokhorenko
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what is the actual need to be used in practice and when?
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And what is the actual need to be used in practice and when?

Writing objects to an ObjectOutputStream, for one...

The Java� Tutorial - I/O: Reading and Writing (but no 'rithmetic)
 
Philip Heller
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexander asked, "And what is the actual need to be used in practice and when?"

Yeah, always a good question!

Suppose you're writing a painting program. Your data model is a vector that contains a bunch of PaintedThingDescriptor instances. PaintedThingDescriptor contains information about color, location, paint extur, etc. What happens when the user clicks "Save"? Without serialization you have to invent a file format - a contract with yourself concerning exactly how all that color, location, etc info gets stored. Then for 3 weeks life is a debugging nightmare because your file reading code has to exactly match what your file writing code did, and thorough testing is hard.

With serialization, you just call writeObject(theDataModelVector), and everything is taken care of for you. Same when you read back.

In addition to this kind of benefit, serialization is the lifeblood of RMI, JDBC, EJBs, and so on. In the simplest case, imagine an RMI call where a program on machine A invokes a method on an object on machine B. The arguments have to get shipped from A to B, and the return value has to come back from B to A. If any of those are objects, they are serialized.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic