user2889419
user2889419

Reputation:

What are essential components for serializing in java?

Assume the following interface in project A

interface Business extends Serializable{//project A
 void do_work();
}

and here we have a class in project B which has implemented the interface

class LocalBusiness implements Business{
 private static final long serialVersionUID = 0xF109BAC00L;
 void do_work(){
  System.out.print("Hi there\n");
 }
}

then I'm going to marshal(serialize) an instance of LocalBusiness at a certain of state.

My question is, I want to unmarshal it in project C, so does project C needs(classpath) the actual implementation of Business interface?

Upvotes: 1

Views: 86

Answers (2)

Rinke
Rinke

Reputation: 6342

Yes, it should. The class name of the run-time type is serialized by the default serialization mechanism. This class name is used to determine the run-time type on deserialization. If Java can't find the class with the received name (and serialVersionUID) it'll throw an exception. Try, and you'll see.

On a side note: your interface extends Serializable. This is considered bad practice in most circumstances.

Upvotes: 3

Robin Green
Robin Green

Reputation: 33093

Yes. The ObjectOutputStream does not contain any class bytecode for the objects written into it.

However, if you use RMI to invoke methods on another machine, you can use dynamic class loading, which lets one machine load code from the other - or even a third machine.

Upvotes: 2

Related Questions