• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization

 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!!!

Does anyone know why javac lets you compile a class that implements Serializable and has references variables to other objects that doesn't implements that interface?
(If you try do serialize an instance of this class you may fall in runtime)
I think it should be a checked exception!!
There must be a reason, of course... but I did not find it!!

Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable is an interface. All the compiler does is to check that the class implements all methods declared in that interface. Since there are none, every class passes this test.

Using Serializable is more like a promise by you -the author of the class- that the class is actually serializable, i.e. that it doesn't contain references to objects that can't be serialized.

(Besides, the class could have a reference of type "Object", which could point to anything - the compiler wouldn't know what to do about that.)
 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Serializable is an interface. All the compiler does is to check that the class implements all methods declared in that interface. Since there are none, every class passes this test.

Using Serializable is more like a promise by you -the author of the class- that the class is actually serializable, i.e. that it doesn't contain references to objects that can't be serialized.



Thanks for reply!!

A class passes on test only if it(or one of its superclasses) implements the interface, right?

I think that the compiler should ensure that... "if you implements Serializable you can't declare references to other class that doesn't implement it too"

Why not?

[]'s
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the compiler should ensure that... "if you implements Serializable you can't declare references to other class that doesn't implement it too"

Why not?


As I pointed out above, the compiler wouldn't know that - the type could be Object which might point to something that's OK to serialize or something that's not.

But, more importantly, it's not the job of the compiler to know what a particular interface means, beyond ensuring that the relevant methods are implemented. If you read through this discussion on marker/tagging interfaces you'll see that they serve very diverse purposes that are way beyond what a compiler can check statically.
 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... just for fun!!

A reference variable of a class that implements Serializable... could point to an object that doesn't implements?

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. At which point it will no longer be serializable.
 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A implements Serializable {}

class B implements Serializable {
A ref = new A();
}

How could you make ref to "point" to a object that doesn't implements Serializable?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, my bad. I read that as
A reference variable in a class that implements Serializable... could point to an object that doesn't implements?

But it could still point to an object that implements Serializable, but is not actually serializable (because it contains non-transient fields of classes that do not implement it).
[ August 07, 2006: Message edited by: Ulf Dittmer ]
 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's my point... why don't the compiler force it?? Why it let you compile you class and take the risc of fall in runtime?

They invented Generics.... why don't fix it too? Why it is not a checked exception? Or at least generate an warning?

There must be a reason... but I did not find it... yet!!
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the compilation time, no object is created. What compiler knows is the reference type.

Basically the runtime system talk with objects. Compiler talk with references.

So following three lines are same for the compiler (all references are of type Object) but definitely they are different for runtime system as they are diferent instances.



Since instances are created at runtime, so it should be the responsibility of the runtime system to check whether instance.getClass() is implementing the serializable interface or not? Not the compiler.

Compiler already knows that all the three object references are not serializable as they all are of type Object and java.lang.Object class does not implement Serialziable interface.

Is there any point making it checked?


Naseem
 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Naseem!!!

Point = Avoid as many problems in run-time as possible. i.e. the compiler check every method invocation... why? to certificate that method(with that arguments) exists... so ... the code works!!

What is the point in declare that a class implements Serializable if it contains references variables to classes that doesn't implements too?

"Maybe you can serialize.... maybe not!! Good Luck!!" ???

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!