aspose file tools*
The moose likes Beginning Java and the fly likes What if I dont implement the serializable interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What if I dont implement the serializable interface ?" Watch "What if I dont implement the serializable interface ?" New topic
Author

What if I dont implement the serializable interface ?

deepak mishra Singh
Greenhorn

Joined: Oct 23, 2008
Posts: 3
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 ?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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.
Victor Ewert
Greenhorn

Joined: May 28, 2009
Posts: 17
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

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.


Steve
deepak mishra Singh
Greenhorn

Joined: Oct 23, 2008
Posts: 3
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 ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Mar 09, 2009
Posts: 600
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
deepak mishra Singh
Greenhorn

Joined: Oct 23, 2008
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What if I dont implement the serializable interface ?