2

This is the class that's about to be serialized into a byte array.

public class DummyClass implements Serializable
{
    private static transient final long serialVersionUID    = -8483859843874771619L;
    public String   y;

    public DummyClass(String y)
    {
        this.y = y;
    }
    public String getY()
    {
        return this.y;
    }
}

This is the Serialization execution test

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] bs = null;
    try
    {
        DummyClass dummyClass = new DummyClass("World I Salute you");
        out = new ObjectOutputStream(bos);
        out.writeObject(dummyClass); // <--- Throws exception here
        bs = bos.toByteArray();
    }
    finally
    {
        out.close();
        bos.close();
    }

and regarding the stack trace:

java.io.NotSerializableException: tests.DummyClassTest
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at tests.DummyClassTest.dummyTest(DummyClassTest.java:109)

The Obvious question is what am i doing wrong?

Cheers.

EDIT:

The whole Class

public class DummyClassTest
{
    public class DummyClass implements Serializable
    {
        private static transient final long serialVersionUID    = -8483859843874771619L;
        public String   y;

        public DummyClass(String y)
        {
            this.y = y;
        }
        public String getY()
        {
            return this.y;
        }
    }

    @Test
    public void dummyTest() throws IOException
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = null;
        byte[] bs = null;
        try
        {
            DummyClass dummyClass = new DummyClass("World I Salute you");
            out = new ObjectOutputStream(bos);
            out.writeObject(dummyClass);
            bs = bos.toByteArray();
        }
        finally
        {
            out.close();
            bos.close();
        }
    }
}
6
  • 3
    I don't think this is all the relevant code. Where is DummyClassTest defined? Commented Nov 16, 2012 at 7:52
  • You mean the DummyClassTest All this code is in that class. Is a JUnit test. Commented Nov 16, 2012 at 7:54
  • 1
    Is your DummyClass a separate class or defined within DummyClassTest? Commented Nov 16, 2012 at 7:54
  • 2
    You are trying to serialize a different class than DummyClass, which I supose is not serializable. Commented Nov 16, 2012 at 7:55
  • 1
    BTW exceptions are throw, not 'cast'. Commented Nov 16, 2012 at 7:56

1 Answer 1

8

A non-static inner class has an implicit reference to its outer class instance. So when you serialize DummyClass, you're also serializing this implicit reference to the DummyClassTest instance owning it.

Define the DummyClass as a top-level class (non nested), or as a static inner class, and everything will work as expected.

The following:

public class DummyClassTest {
    public class DummyClass {
    }
}

is more or less equivalent to

public class DummyClassTest {
}

public class DummyClass {
    private DummyClassTest outerInstance;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's the stupidest thing I've ever wrote. Thanks I forgot to test that. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.