Skip to content

Instantly share code, notes, and snippets.

@NAVNEETOJHA
Created March 23, 2021 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NAVNEETOJHA/734cc7a43dc01840ad0400aa7d501734 to your computer and use it in GitHub Desktop.
Save NAVNEETOJHA/734cc7a43dc01840ad0400aa7d501734 to your computer and use it in GitHub Desktop.
import org.apache.commons.lang3.SerializationUtils;
import java.io.Serializable;
// some libraries use reflection (no need for Serializable)
class Foo implements Serializable
{
public int stuff;
public String whatever;
public Foo(int stuff, String whatever)
{
this.stuff = stuff;
this.whatever = whatever;
}
@Override
public String toString()
{
return "Foo{" +
"stuff=" + stuff +
", whatever='" + whatever + '\'' +
'}';
}
}
class CopyThroughSerializationDemo
{
public static void main(String[] args)
{
Foo foo = new Foo(42, "life");
// use apache commons!
Foo foo2 = SerializationUtils.roundtrip(foo);
foo2.whatever = "xyz";
System.out.println(foo);
System.out.println(foo2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment