11 Replies Latest reply: Jul 21, 2011 9:22 AM by 866541 RSS

    Object serialization process?

    866541
      Object serialization is the process of saving an object's state to a sequence of bytes. Does it saves only the instance variable or also the object methods(like getter and setter methods) ? Once we we write the object to outputstream or some text file, how does it get transmitted over network?
      Does we write the java object to text file only duuring serialization?

      Edited by: JavaFunda on Jun 17, 2011 12:37 PM
        • 1. Re: Object serialization process?
          DrClap
          It saves the data for an instance of the class. Of course it doesn't save the methods, because they don't belong to an instance of the class. Neither do static variables, and therefore they aren't serialized either.
          Once we we write the object to outputstream or some text file(not sure if it is text file or class file?), how does it get transmitted over network?
          It isn't text (as you can immediately see if you serialize an object to a file and look at what you did).

          If you serialize an object to a file, then it gets written to a file. In that case it doesn't get transmitted over any networks. If you serialize to an output stream which is connected to something across a network, then naturally the serialized data gets transmitted over that network. However that has nothing specifically to do with serialization of objects, it true no matter what data you write to that file or output stream.
          • 2. Re: Object serialization process?
            796440
            JavaFunda wrote:
            Object serialization is the process of saving an object's state to a sequence of bytes. Does it saves only the instance variable or also the object methods(like getter and setter methods) ?
            Only the state--the instance variables. It doesn't save the class definition. That has to be available separately (via classloader) at deserilaization time. In other words, you cannot deserialize an instance of a class that is not on your classpath.
            Once we we write the object to outputstream or some text file, how does it get transmitted over network?
            The same way any other bytes get transmitted. You have a Socket. You get its OutputStream. You wrap that in an ObjectOutputStream. When you write to the ObjectOutputStream, that writes through to the Socket's OutputStream, which is responsible for putting the bytes on the wire.
            Does we write the java object to text file only duuring serialization?
            We write the objects to wherever the other end of the ObjectOutputStream is connected to. Just like any other I/O.
            • 3. Re: Object serialization process?
              DarrylBurke
              Cross posted
              http://www.coderanch.com/t/542178/Streams/java/Object-Serialization-Process

              db
              • 4. Re: Object serialization process?
                796440
                Darryl Burke wrote:
                Cross posted
                http://www.coderanch.com/t/542178/Streams/java/Object-Serialization-Process

                db
                JavaFunda, please don't do that. It's rude, as it leads people to waste their time repeating answers that others have already given elsewhere.

                If you continue to crosspost, I will stop answering your questions. It's likely that others will do the same.
                • 5. Re: Object serialization process?
                  866541
                  Extremely sorry for that.Happened because of some misunderstanding .Wont happen again.

                  Edited by: JavaFunda on Jun 18, 2011 2:20 AM

                  Edited by: JavaFunda on Jun 18, 2011 2:22 AM
                  • 6. Re: Object serialization process?
                    796440
                    No worries.Thank you for understanding.
                    • 7. Re: Object serialization process?
                      866541
                      jverd wrote
                      Only the state--the instance variables. It doesn't save the class definition. That has to be available separately (via classloader) at deserilaization time. In other words, you cannot >>deserialize an instance of a class that is not on your classpath.
                      so during Deserialization,new object will be created at serverside with same values of instance variables lying in persisted object. correct?

                      Edited by: JavaFunda on Jun 20, 2011 10:37 AM
                      • 8. Re: Object serialization process?
                        802316
                        JavaFunda wrote:
                        so during Deserialization,new object will be created at serverside with same values of instance variables lying in persisted object. correct?
                        It reads the class name, creates an instance of that class, reads the field names and their values and sets each value in turn.
                        It remembers each object it has created as the same object can appear more than once.
                        • 9. Re: Object serialization process?
                          866541
                          what if we have just interface on client side and actual definition on some remote machine. Fo example in case of EJB home/Remote interface. How we will get the actual definition and object on client side at run time.
                          • 10. Re: Object serialization process?
                            EJP
                            In that situation you don't need it, do you, because it is remote, and the container organizes a remote stub for you. The actual implementation object remains at the server along with its .class file.
                            • 11. Re: Object serialization process?
                              866541
                              Hi EJP i am under impression that (taking the example of EJB) when we do HomeInterface hi=jndi.lookup("Home") // getting home object here and then RemoteInterface ri= hi.create();// getting remote object here. Call goes to server and it gets the implementation from serverside.

                              But looks like from your post these implementation are created before run time (probably at deployment time) in the form of stubs and call does not go to server side. Is that correct?

                              If yes As per my understanding these stubs need to be handed over to client side by server side. Right?