About Question enthuware.ocpjp.v7.2.1390 :

Moderator: admin

Post Reply
Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

About Question enthuware.ocpjp.v7.2.1390 :

Post by Danny Sheridan » Sat Sep 12, 2015 12:23 pm

Is it okay to declare throws Exception for these methods?
Doesn't this feature need the specific throws clauses ?

Code: Select all

private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

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

Post by Danny Sheridan » Sat Sep 12, 2015 12:49 pm

I think I see where I was mistaken.
In interface Serializable docs it says:
Classes that require special handling during the serialization and deserialization process
must implement special methods with these exact signatures
exact signatures - that's just method name and parameter types and order
But not the throws declaration
So when the JVM checks by reflection for these methods it identifies them by signature.
So any throws that covering the exceptions thrown would do.
Is this on the right track?

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

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

Post by admin » Sat Sep 12, 2015 7:27 pm

You are correct. Throws clause becomes important when you override a method.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

Post by zhengye1 » Wed Oct 17, 2018 2:50 pm

I try to create the program to understand this question, however when I try to run the program I got

Code: Select all

Exception in thread "main" java.io.NotActiveException: not in call to writeObject
	at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:436)
	at ocp.wrongq.Portfolio.writeObject(Portfolio.java:29)
	at ocp.wrongq.Portfolio.main(Portfolio.java:46)
Here is my program:

Code: Select all

// In Portfolio.java
package ocp.wrongq;

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

class Bond {

	String ticker = "bac";
	double coupon = 8.3;

	@Override
	public String toString() {
		return "Bond [ticker=" + ticker + ", coupon=" + coupon + "]";
	}
}

public class Portfolio extends Bond implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2252868398846453877L;
	String accountName;
	transient Bond bond = new Bond();

	private void writeObject(ObjectOutputStream os) throws Exception {
		os.defaultWriteObject();
		os.writeObject(bond.ticker);
		os.writeDouble(bond.coupon);
	}

	private void readObject(ObjectInputStream os) throws Exception {
		os.defaultReadObject();
		this.bond = new Bond();
		bond.ticker = (String) os.readObject();
		bond.coupon = os.readDouble();
		System.out.println(this.bond);
	}

	public static void main(String[] args) throws Exception{
		Portfolio p = new Portfolio();
		FileOutputStream fos = new FileOutputStream("t.tmp");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		p.writeObject(oos);
		FileInputStream fis = new FileInputStream("t.tmp");
		ObjectInputStream ios = new ObjectInputStream(fis);		
		p.readObject(ios);
	}
}
What did I do wrong?

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

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

Post by admin » Wed Oct 17, 2018 10:34 pm

You need to change p.writeObject(oos); to oos.writeObject(p);
and p.readObject(ios); to ios.readObject(p);
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

Post by zhengye1 » Wed Oct 17, 2018 10:51 pm

But if we did this change, how to invoke that two private method?

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

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

Post by admin » Wed Oct 17, 2018 11:11 pm

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

bortnikov_sa@mail.ru
Posts: 8
Joined: Wed Oct 25, 2017 2:53 am
Contact:

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

Post by bortnikov_sa@mail.ru » Thu Dec 06, 2018 5:55 am

Hellow. Is it ok to use: os.writeObject(accountName) and accountName = (String) os.readObject() instead os.defaultWriteObject() and os.defaultReadObject() respectively?

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

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

Post by admin » Thu Dec 06, 2018 9:54 am

No, both the methods have different purpose. defaultReadObject can only be called from within the implementation of readObject. Please take look at their API description.
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 11 guests