Skip to content

Instantly share code, notes, and snippets.

@cattaka
Created November 3, 2016 06:29
Show Gist options
  • Save cattaka/4738dedfcd4336a57e3d3c0df9b9eb06 to your computer and use it in GitHub Desktop.
Save cattaka/4738dedfcd4336a57e3d3c0df9b9eb06 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
/**
* Created by cattaka on 2016/11/03.
*/
public class NotSerializableExceptionExample {
public static void main(String[] args) throws Exception {
new NotSerializableExceptionExample().func();
}
public void func() throws Exception {
System.out.println(new ArrayList<String>().getClass().toString()); // -> class java.util.ArrayList
System.out.println(new ArrayList<String>() {}.getClass().toString()); // -> class NotSerializableExceptionExample$1
System.out.println(new ArrayList<String>() {{ add("hoge"); }}.getClass().toString()); // -> class NotSerializableExceptionExample$2
Object obj1 = new ArrayList<String>();
Object obj2 = new ArrayList<String>() {{}};
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(obj1); // OK
oout.writeObject(obj2); // NotSerializableException
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment