Is Java automatically executing Serializable classes if they are used in the Serializable interface?

Say I have a Serializable interface as follows

public interface SomeInterface extends Serializable { public SomeClass getSomething(String someParameter); } 

Will SomeClass automatically serialized? To our surprise, our RMI application works just fine even when SomeClass does not implement Serializable.

Why is this?

+5
source share
4 answers

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

No.

Serialization is the serialization of the (non-transitional) state of an object. The presence of getSomething in the signature of a method does not require the state of your SomeInterface instance to include an SomeClass instance.

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

+2
source

No. Any class just mentioned in a class that is serializable is not serializable by default.

Ideally, java should throw a NotSerializableException , but since you are not getting an exception, SomeClass extends Serializable by is a parent hierarchy somewhere.

0
source

No, SomeClass will not be Serializable just because this interface extends Serializable .

Serializable applies to the state of an object, there is nothing here that would imply that SomeClass is part of the state of SomeInterface. https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

Do you think that when SomeClass deserialized, it does not extend the class that implements Serializable ?

-1
source

Suppose the implementation of SomeInterface just does something similar to

 public class SomeInterfaceImpl implements SomeInterface { @Override public SomeClass getSomething(String someParameter) { return new SomeClass(someParameter); } } 

Why SomeInterfaceImpl your program fail to serialize SomeInterfaceImpl objects when SomeClass objects are created only on the fly?

Minimal example

Main.java

 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.logging.Level; import java.util.logging.Logger; public class Main { public static void main(String[] args) { try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream)) { final SomeInterfaceImpl someInterfaceImpl = new SomeInterfaceImpl(); final SomeClass something1 = someInterfaceImpl.getSomething("Test"); out.writeObject(someInterfaceImpl); try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); final ObjectInputStream in = new ObjectInputStream(byteArrayInputStream)) { final SomeInterface someInterface = (SomeInterface) in.readObject(); final SomeClass something2 = someInterface.getSomething("Test"); final String someParameter1 = something1.getSomeParameter(); System.out.println(someParameter1); final String someParameter2 = something2.getSomeParameter(); System.out.println(someParameter2); } catch (ClassNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } 
-1
source

Source: https://habr.com/ru/post/1259473/


All Articles