Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Serializing Methods

It is possible to serialize a method? Because when I look at the documentation the Method class does not implements Serializable. So are there any alternatives?

like image 205
Richeve Bebedor Avatar asked Feb 07 '11 08:02

Richeve Bebedor


People also ask

Can a method be serialized in Java?

Since Method does not implement Serializable, it cannot be serialized using the standard Java Serialization API.

How many ways we can serialize in Java?

Java Serialization Methods That's why there are four methods that we can provide in the class to change the serialization behavior.

Can methods be serialized?

Serialization is a mechanism of storing the state of an object. Based on this definition we can say that the instance variables in an object can be serialized. Methods are behaviors of the class. We can set and get the state of an object using the methods.

Which method is used for serialization in Java?

To make a Java object serializable we implement the java. io. Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.


1 Answers

Since Method does not implement Serializable, it cannot be serialized using the standard Java Serialization API.

A workaround would be to manually serialize just the name of the class and method and its parameter types. You can then recreate the Method instance during deserialization.

like image 79
dogbane Avatar answered Nov 09 '22 15:11

dogbane