-2

Can any on explain me in which condition JVM throw notserializableexception.

exmaple

class Emp implemenst Serializable
{
   Address address = new Address();
}

class Address 
{
   Strign address;
}

in above case does JVM will throw the exception because address class is not serializable?

can anybody explain?

2 Answers2

1

The problem is that Address doesn't implement Serializable, so you have to implement it or mark address inside of Emp as transient, which basically means ignore the member from serialization.

Read here: https://en.wikibooks.org/wiki/Java_Programming/Keywords/transient

--> Java will throw the exception if a member doesn't implement Serializable or isn't transient.

huellif
  • 414
  • 4
  • 12
1

If you want to serialize an object in Java, each field in that object (and its fields, etc..) must implement Serializable interface. We could say that each object is serialized separately.

Thats why Java throws this exception in your case - one of the fieds is not Serializable.

Mateusz Stefaniak
  • 856
  • 1
  • 9
  • 20