About Question enthuware.ocpjp.v7.2.1703 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
biz800
Posts: 1
Joined: Fri Aug 09, 2013 9:11 pm
Contact:

About Question enthuware.ocpjp.v7.2.1703 :

Post by biz800 »

public class Student implements Serializable{     
public static final int serialVersionUID = 1;     
public String id="S111";     
public String name;     
public String grade;     
public int age=15;     
public String toString(){ return "["+id+", "+name+", "+grade+", "+age+"]"; } }

"Since the serialVersionUID of the serialized class and the new class are same, the file will be deserialized without any issue. The new fields will be initialized to their Java defaults (because constructors and initializers are not invoked during deserialization). So the values for id and and age will remain null and 0 respectively."


I understand that the constructors and initializers are not invoked, but this class Student has default values set to id="S111" and age=15. When trying to read the object, why is the id= null and age = 0?

Thank you

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by admin »

Because field initializers are not invoked during deserialization. So the code id = "S111"; never gets executed when you deserialize a Student object.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by Student »

It might also be worth mentioning that serialVersionUID must be [any access modifier] static final long serialVersionUID.
When testing out this concept out I accidentally omitted one of those (can't remember which...maybe "final") and the behaviour changed from what was expected. When I noticed and corrected the error it worked as specified.
Cheers.

Alina_Lapina
Posts: 15
Joined: Tue Jan 13, 2015 12:10 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by Alina_Lapina »

From another source I've learned how the constructors are invoked during deserialization:

Code: Select all

//imports

class Grandparent {
public Grandparent() {System.out.println("A");}
public Grandparent(int number) {System.out.println("1");}}

class Parent extends Grandparent {
public Parent() {System.out.println("B");}
public Parent(int number) {super(number);System.out.println("2");}}

public class Onion extends Parent implements Serializable {
	public Onion() {System.out.println("C");}
	public Onion(int number) {super(number);System.out.println("3");}

	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("child"));
		out.writeObject(new Onion(11));

		ObjectInputStream in = new ObjectInputStream(new FileInputStream("child"));
		System.out.println(in.readObject());
	}
}
the output will be:

Code: Select all

1
2
3
A
B
Onion@25fe6783
Is it different situation? Why the constructors are invoked here?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by admin »

During deserialization, constructor will be invoked for the first superclass class in the hierarchy that does not implement Serializable. This constructor will of course invoke its superclass's constructor as well.

Which book are you following? It should have explained this.

Paul.
If you like our products and services, please help us by posting your review here.

Alina_Lapina
Posts: 15
Joined: Tue Jan 13, 2015 12:10 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by Alina_Lapina »

admin wrote:...
Which book are you following? It should have explained this.
Paul.
It's Niko's java blog, no explanation. https://nikojava.wordpress.com/2008/10/ ... ock-exams/

Thanks for your answer!

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by crazymind »

It is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException.
What are the sender and receiver in this context?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by admin »

They could be any two programs.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by jme_chg »

1. When a file is deserialized into an object, the class's constructor and instance initializers are not called. So the fields for which no value is available in the serialized file, are initialized to their default values (i.e. number fields to 0, boolean to false, and references to null).

^is the bold part referring to transient variables that were ignored in serialization?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by admin »

Yes, transient fields plus the instance fields of an unserializable super class (which will be initialized by the no-args constructor of the unserializable superclass).
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by jme_chg »

admin wrote:
Fri Apr 02, 2021 7:28 am
Yes, transient fields plus the instance fields of an unserializable super class (which will be initialized by the no-args constructor of the unserializable superclass).
if that's the case then
1. When a file is deserialized into an object, the class's constructor and instance initializers are not called. So the fields for which no value is available in the serialized file, are initialized to their default values (i.e. number fields to 0, boolean to false, and references to null).

is not fully correct as instance fields of an unserializable super class will be initialized in the super class' constructor so will not take default value.

e.g.

Code: Select all

class B { //B not Serializable so its properties cannot be serialized…
    int i; public B() { i = 10; }
}
class D extends B implements Serializable {
    int j; public D() { j = 20; }
}
class T extends D { //D implements Serializable so T also Serializable…
    int k; public T() { k = 30; }
}
And:
T t = new T();
t.i = 100;
t.j = 200;
t.k = 300;
Then after serialization and deserialization to tDeserialized:
tDeserialized.i = 10 //T’s parent B not Serializable, so t.i NOT serialized… but is 10 as B() constructor called in deserialization
tDeserialized.j = 200
tDeserialized.k = 300

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1703 :

Post by admin »

Actually, the explanation is about the class for which constructor and instance initializers are not called. That is why the first sentence of the explanation says, "When a file is deserialized into an object, the class's constructor and instance initializers are not called."

But if you take the bold part out of context i.e. "So the fields for which no value is available in the serialized file, are initialized to their default values", then yes, it does not remain entirely true. Which is why I mentioned the unserializable super class fields.

Will enhance the explanation to make it clear.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests