File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes serialization with inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "serialization with inner classes" Watch "serialization with inner classes" New topic
Author

serialization with inner classes

Prashant Khurpade
Greenhorn

Joined: Sep 03, 2008
Posts: 9
hi all,

class A implements serializable{ // main class

class B{ // inner class


}

}

whether object of class B will also get serialized?

thanks in advance
Prashant
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Nope! An inner class is not part of outer class(for serialization). Infact the inner class has-a outer class instance.

You can see my blog if you are confused about inner classes...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
No, Inner Class will not be considered as seralizable, it will give runtime Exception :java.io.NotSerializableException.

refer Following Two example:

1:- Will Throw NotSerializableException.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class SerializeOuterClass implements Serializable{

public int i =10;
public InnerClass innerClass = new InnerClass();
class InnerClass {
public int j=10;
}

public static void main(String[] args) throws Exception{
SerializeOuterClass hi = new SerializeOuterClass();
hi.innerClass.j = 20;
ObjectOutputStream oOut = new ObjectOutputStream(new FileOutputStream("test.ser"));
oOut.writeObject(hi);
oOut.close();

ObjectInputStream oIn = new ObjectInputStream( new FileInputStream("test.ser"));
SerializeOuterClass HiReaded = (SerializeOuterClass) oIn.readObject();
oIn.close();
System.out.println(HiReaded.innerClass.j);
}


}

2:- Working fine

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class SerializeOuterClass implements Serializable{

public int i =10;
public InnerClass innerClass = new InnerClass();
class InnerClass implements Serializable {
public int j=10;
}

public static void main(String[] args) throws Exception{
SerializeOuterClass hi = new SerializeOuterClass();
hi.innerClass.j = 20;
ObjectOutputStream oOut = new ObjectOutputStream(new FileOutputStream("test.ser"));
oOut.writeObject(hi);
oOut.close();

ObjectInputStream oIn = new ObjectInputStream( new FileInputStream("test.ser"));
SerializeOuterClass HiReaded = (SerializeOuterClass) oIn.readObject();
oIn.close();
System.out.println(HiReaded.innerClass.j);
}


}


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

kaushik you are referring to a different case where the enclosing class has an instance of the inner class. But if the enclosing class has no instance of the inner class then the inner class will not be serialized and thus no exception will be thrown even if the inner class is not serializable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: serialization with inner classes