• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class & serialize  RSS feed

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A non static inner class can be serialized or not ?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, an instance of a non-static inner class may be serialized.
[ June 30, 2004: Message edited by: Joe Ess ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok my fault, my meaning was :
A inner class can not be serialized (ad deserialized) because a no static inner calss can not have static members (and for serilize a class needs a public static final long verionUI).The code below argue this.

public class SerilizeInnerClass {

private final String fileName = "out.buff";


public SerilizeInnerClass() {
System.out.println("Serialize start");
serialize();
System.out.println("Serialize end");

// System.out.println("Deserialize start");
// deserialize();
// System.out.println("Deserialize end");
}

public static void main (String args []) {
new SerilizeInnerClass();
}

private class Inner implements Serializable {
int i = 10;

public void print() {
System.out.println();
}
}

private boolean serialize() {

boolean ret = false;

Inner inner = new Inner();

FileOutputStream fo = null;
BufferedOutputStream oBuff = null;
ObjectOutputStream oo = null;

try {
fo = new FileOutputStream(fileName);
oBuff =new BufferedOutputStream(fo);
oo = new ObjectOutputStream(oBuff);

oo.writeObject(inner);

fo.close();
oBuff.close();
oo.close();

} catch (IOException ioEx) {
ioEx.printStackTrace();
}

fo = null;
oBuff = null;
oo = null;
ret = true;
return ret;
}

private void deserialize() {
FileInputStream fi = null;
BufferedInputStream iBuff = null;
ObjectInputStream oi = null;

try {

fi = new FileInputStream(fileName);
iBuff = new BufferedInputStream(fi);
oi = new ObjectInputStream(iBuff);

Object get = oi.readObject();

fi.close();
iBuff.close();
oi.close();

} catch (IOException ioEx) {
ioEx.printStackTrace();
} catch (ClassNotFoundException cnfEx) {
cnfEx.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

fi = null;
iBuff = null;
oi = null;
}
}

The code can be compiled but on run time an java.lang.UnsupportedClassVersionError raise.

As far as I know ,there is a way to trick this but this make your code hard to undestand.

And the question remains :
The non static inner classes can be serialized ? If yes how ?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run your code, I get a java.io.NotSerializableException: SerilizeInnerClass. And it's true. That class is not serializable. I implement Serializable in the class declaration, remove the problematic stream close() invocations and it works fine:
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok you are right.But why ?
If the main class(in out case the outer class) is serializable all its inner class inherit this ?
It is logic - a inner class can be a part from the outer class state, and the state is serialized.
Is this right or there are others reasons ?
 
Vinod Subramaniam
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only Primitive types in java gets serialised automatically. If you wnat to serialize any object its type must implement serializabale interface and hence as in the previous case when the inner class imaplemented the seraializable interface it was possible for serialising. The non-static inner class is a member of a class and does not inherit the properites of the Enclosing class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!