11 Replies Latest reply: Apr 8, 2013 5:28 AM by Murray9654 RSS

    small doubt on serialization?

    Murray9654
      Hi
      i am serializing an object which implements serializable using ObjectOutputStream but that object class does not have a no argument constructor. When it is being deserialized the object is getting constructed back again. but how is this possible? "Class.forName" also requires the object class to have a no args constructor. so in deserialization how the object is constructed back again even when it does not have a no args constructor? I am trying to understand how this is being possible? can any one help me understand the logic?
        • 1. Re: small doubt on serialization?
          jtahlborn
          serialization requires special help from the jvm. it uses internal jvm magic to create objects without using constructors.
          • 2. Re: small doubt on serialization?
            Murray9654
            ya It must be the case. even the instance blocks and constructors are not getting called when deserializing the object. This mean The object is not getting constructed in a normal way. so it must be jvm specific. so if one jvm serializes it, that cannot be deserialized by a different jvm. Is it true? (Think that I have two jvm's from different organizations. one from oracle and one from IBM).
            • 3. Re: small doubt on serialization?
              baftos
              No. The serialization format is well defined and not VM dependent. You serialize on machine A with VM X and deserialize on machine B with VM Y and you should be ok.
              The rule is that a Serializable class must inherit only Serializable classes and if not, the first (most recent) non-Serializable ancestor must have the no-args constructor.
              This constructor may be explicitly declared or not (in case other constructors are not there, the no-args constructor is an implicit do-nothing constructor).
              • 4. Re: small doubt on serialization?
                Murray9654
                are u sure about this? it is because we are facing some problem in deserializing an object on android(dalvik vm) which has been serialized by oracle VM(hotspot) the default VM for j2se. and if it is not VM dependent can u explain the logic how an object is being deserialized(constructed back) without calling constructors or initialization blocks. simple explanation is good enough. Thanking you in advance.

                also please refer to the following thread as well

                http://stackoverflow.com/questions/15863585/serialization-in-android

                Edited by: Muralidhar on Apr 8, 2013 5:15 AM
                • 5. Re: small doubt on serialization?
                  EJP
                  are u sure about this?
                  I am entirely sure about this. That's precisely what it says in the Object Serialization Specification.
                  because we are facing some problem
                  What problem? If you mean 'the instance blocks and constructors are not getting called', that is correct behaviour.
                  if it is not VM dependent can u explain the logic how an object is being deserialized(constructed back) without calling constructors or initialization blocks.
                  Because that's what the Specification requires it to do. That's what makes it VM-independent. The specification.
                  simple explanation is good enough.
                  You don't need an explanation. It's required behaviour. The JVM has to comply. How the JVM accomplishes it isn't relevant. What you need is a solution to your problem, whatever it is.
                  please refer to the following thread.
                  http://stackoverflow.com/questions/15863585/serialization-in-android
                  Why? All it does is state a major error. See my comment there.
                  • 6. Re: small doubt on serialization?
                    baftos
                    Honestly, while I have some Android experience, I don't know if their serialization format is compatible with the official Java's. If it is not, they are kind of stupid, as far as I am concerned, unless someone proves me wrong.
                    Keep in mind that Android != Java. They have a lot in common: syntax and many API-s, but maybe not the serialization format(?). Anyway, the link you mention has a very good point: don't serialize long term using Java's default serialization, it may hunt you when the other component is or may turn to be Android, .NET or whatever.
                    • 7. Re: small doubt on serialization?
                      Murray9654
                      Thank you so much. The last doubt is again confusion with baftos post. As baftos also says android does not fully comply with java spec could there be any issue with android (dalvik) VM?

                      Edited by: Muralidhar on Apr 8, 2013 8:07 AM
                      • 8. Re: small doubt on serialization?
                        EJP
                        If it isn't compatible with JDK Serialization it is a bug in Android.
                        • 9. Re: small doubt on serialization?
                          Murray9654
                          Thank you so much. I am done with my doubt. very clear in mind.

                          One last doubt I have on serialization is, as object is getting created without calling constructors and initialization blocks in deserialization, does programmer also have a chance to create an object without calling constructors and int blocks? I mean can a programmer will be able to do that?
                          • 10. Re: small doubt on serialization?
                            EJP
                            No.
                            • 11. Re: small doubt on serialization?
                              Murray9654
                              Thank you so much.