2

Possible Duplicate:
Why is a serializable inner class not serializable?

I have a class that does not implement Serializable. Inside of that class I have a data class which holds data for the whole program which implements Serializable and has a version ID.

My problem is that when I try to serialze a data object with in my main class I get a object write error from the main class and not the data class. The main class does not implement Serializable.

Can you serialize a class within a non-serializable class?

Example:

class main
{
   class data implements Serializable
   {  // data and functions }

   public void main(args)
   { data d = new data();
     // ofcourse I have the proper inits and checks for the output stream and such
     writeObject(data);  // Throws Class not Serializable error.}
} 
Community
  • 1
  • 1
AnotherUser
  • 1,311
  • 1
  • 14
  • 35

2 Answers2

6

As data is not static it implicitly has a reference to its outer class main which is not Serializable.

Try making the class static.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I am not new to java by no means but making the class static has what presumptions? Why does making it static work? – AnotherUser Jan 09 '13 at 17:06
  • static means it doesn't have an implict reference to the outer class i.e. it removes a reference to a class which is not Serializable. BTW I would make every nested classes `static` if you can. – Peter Lawrey Jan 09 '13 at 17:06
  • @user1596244 one difference code-wise is that if you were to access your `data` class from outside `main`, you would need an instance of `main` in order to instantiate `data`, like in (ewww) `new main().new data()`. If `data` is static, you just have to `new main.data()` without having to have an instance of `main`. – fge Jan 09 '13 at 17:11
  • A member of my very outer non-serial class is data d = new Data();. Making data class static,, does this mean that methods and functions within the outer class can or cannot still access this data d object. I figure it has no problem. (TYPO in OP, I have data d, outside of main) – AnotherUser Jan 09 '13 at 17:14
  • Making `data` static would mean it cannot reference a `main` as this is not Serializable. – Peter Lawrey Jan 09 '13 at 17:16
  • So with it static, it cannot access any fields or functions in its encapsulating/outer class. But fields and methods in the outer class can access fields and methods in the data class, right? --- This is good, I only want data to hold data. Let me write to it, and read from it, it does not need to access and outer class functions or fields. Is this right? – AnotherUser Jan 09 '13 at 17:19
  • 1
    The outer class can access anything in the inner class if it has a reference to an instance. The inner class can access anything on the outer class if it has a reference to it. What `static` is there is no *implicit* reference to an instance of the outer class. – Peter Lawrey Jan 09 '13 at 17:21
  • So if I do want my inner class to access the outer one, I need to make a method within the inner class to set a reference to a class, the reference pointing to the outer class? – AnotherUser Jan 09 '13 at 17:33
  • You would have to make the reference `transient` so it is not serialized as it can't be and reset it when the class is deserialized. – Peter Lawrey Jan 09 '13 at 17:35
  • Thank you Peter, this makes a whole lot of sense now! – AnotherUser Jan 09 '13 at 17:36
3

If you decompiled your main.class you would see that in the real code

public class main {

    class data implements Serializable {
        final main this$0;

        data() {
            this$0 = main.this;
        }
    }
}

data contains a reference to its outer class main. This so called synthetic field is added by javac. When ObjectOutputStream is trying to serizalize data it stumbles upon a reference to a non-serializable class and breaks. So to be serializable you should make main implement Serializable too, or make data static class then the reference to main will go

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275