Created
November 3, 2016 06:29
-
-
Save cattaka/4738dedfcd4336a57e3d3c0df9b9eb06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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