import java.io.*;
import java.util.*;
// - ponha suas classes Car e Wheel aqui
class TesteSerializacao {
public static void main(String[] args) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("teste.bin"));
Car c = new Car();
c.wheels = new Wheels();
oos.writeObject (c);
oos.close();
}
}
class Wheels { }
class Car implements Serializable {
Wheels w;
}
public class TesteSerializacao {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("teste.bin"));
Car c = new Car();
c.w = new Wheels();
oos.writeObject (c);
oos.close();
resultado :
new Car Exception in thread “main” java.lang.NoSuchFieldError: w
at br.com.TesteSerializacao.main(TesteSerializacao.java:27)
Tentar serializar uma instância de Car e a variável de referência Wheels w for inicializado com valor default(null) , vai serializar sim! sem nenhum erro
Ou seja, a questão é mal-formulada, já que o comportamento depende de o objeto ter sido ou não preenchido de determinada forma, que não foi explicitada no programa dado na questão.