Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java make classes Serializable automatically if used in a Serializable interface?

Let's say I have a Serializable interface as follows

public interface SomeInterface extends Serializable {

     public SomeClass getSomething(String someParameter);

}

Will SomeClass be Serializable automatically? To our surprise, our RMI application runs fine even when SomeClass does not implement Serializable.

Why is this?

like image 239
Auberon Avatar asked Nov 20 '22 12:11

Auberon


1 Answers

Will SomeClass be Serializable automatically? To our surprise, our RMI application runs fine even when SomeClass does not implement Serializable.

No.

Serialization is about serializing the (non-transient) state of an object. The presence of a getSomething in the signature of some method doesn't require the state of your instance of a SomeInterface to include a SomeClass instance.

The method could be implemented to return a newly created SomeClass, the value of a transient field, the result of calling a static method on some other class, or .... null.

like image 102
Stephen C Avatar answered Jun 16 '23 17:06

Stephen C