Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialization with inner classes

 
Prashant Khurpade
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}


}
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic