• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What if I dont implement the serializable interface ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if i dont make a class serializable ? cant it "save" its object or "persist" that object across files ?
i have NEVER implemented the serializable interface while writing my beginner java codes, or even servlets. but still, they worked fine in the way that i could save and share my compiled class files. what exactly would have i gained if i would have implemented the serializable interface ?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak:

You don't have to implement the Serializable interface, if you don't ever intend to serialize your class. In fact, for many classes, using Serializable doesn't make sense. Serialization has nothing to do with sharing class files, though.

John.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand it correctly, serialization is taking an object (class instance) and persisting it to a file that can later be accessed. As the earlier posted said, it has nothing to do with sharing your .class files.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is one use of serialisation. The other use is breaking the object into a series (hence the name) of bytes which can be passed across a network.

If you don't need those functionalities, you don't need Serializable.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just going to add one more thing, because you mentioned Servlets in your original post.

When you are using Servlets there is a lot of work done behind the scenes to make sure the web application works in a memory efficient manner. With some configurations that could mean sending user data from one server to another or storing user data in a database for a while. So if you have any data which you want to put in the ServletContext or Session that data should be Serializable in order to take advantage of these optimizations. If you don't then at some future time your application may break or give you un-expected results.
 
deepak mishra Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:Deepak:

You don't have to implement the Serializable interface, if you don't ever intend to serialize your class. In fact, for many classes, using Serializable doesn't make sense. Serialization has nothing to do with sharing class files, though.

John.



i think i failed to present my question properly. i would reframe it.
the first "hello world" program i wrote, never implemented the serializable interface. still, i could save/ persist the class file on my harddisk,or across the network (which is what is serialization supposed to do). how was that possible ? what difference would it have made if it implemented the serializable interface ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How was it possible? Because as John de Michele said, serialization has nothing to do with saving compiled class files. A class file is simply a file in your operating system. You can do anything with that file which you can do with any other file, regardless of what you put in your source code. All you need is source code which compiles.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak mishra Singh wrote:i think i failed to present my question properly. i would reframe it.
the first "hello world" program i wrote, never implemented the serializable interface. still, i could save/ persist the class file on my harddisk,or across the network (which is what is serialization supposed to do). how was that possible ? what difference would it have made if it implemented the serializable interface ?



No, you stated your question plainly enough. I think the problem is that you're confusing a .class file with a serialized object. A class file is the bytecode generated by the Java compiler. You can pass this around as much as you want. Serialization works on object instances, and saves the state of that object instance in a standard way that can be saved as a file or sent across a network (e.g., using RMI). If you didn't use one of the object writing classes and the Serializable interface, then your 'hello world' program was not serialized. You can take a look at the Java serialization spec here.

John.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization refers to saving the STATE of an object, not the class file itself. in other words, if you created a Student object, it may be assigned a name, DOB, SSN, address, etc. When your program shuts down, all that stuff is gone.

If the object is serializable, you can write a small file that records all that info to disk. The JVM can shut down - heck, the whole box can be turned off. When you re-start the JVM, you can read that file, and re-build a specific instance as it was before the shutdown.

This same file can be sent to OTHER JVMs, via a socket, an email, a floppy disk, etc. As long as that second JVM knows about the class (i.e. has the class file), it too can restore the STATE of the object.
 
deepak mishra Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all of you, now i get the point. it was really silly of me to confuse between class and object
but luckily i studied more about it and gave serialization a try, by writing 2 simple programs, one to serialize the current time in a file, and the other one to deserialize/retrieve it. now i have some more queries :

1.the first program serialized current time to a save.ser file. i couldnt grasp what was in it....was it bytecode (as it looked similar to my .class file) or something else ?

2.

fred rosenberger wrote: As long as that second JVM knows about the class (i.e. has the class file), it too can restore the STATE of the object.


my second program (the one used for deserializing) never used the first class file(the one used for serialization), still it could deserialize the contents of save.ser. is the above highlighted quote incorrect ?

3. since the object (and not the class) is not in bytecode and is perhaps in bits/bytes in memory, the object we serialize can be deserialized using any language (even the ones which dont need a virtual machine), right ?

4. is serialization about saving the "entire object", or about saving the "information necessary to recreate the object" ?i feel the right answer is the second one, but wont it be good if we save the entire object (its just a collection of bits and bytes afterall !). if we store the object itself, we wont need any language compiler/jvm, etc. for reusing that object in future.


i am sorry if i again sound very silly... but i hope you will clear my doubts
reply
    Bookmark Topic Watch Topic
  • New Topic