GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization" Watch "Serialization" New topic
Author

Serialization

Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11
Hello.
Please see the following code. I wrote the code by myself. But it may look like the code on Serialization in K & B book.


Please see that two lines are marked in boldface.
I have a query on this code.

1. If I comment out Line 1 and Line 2 ( in boldface ) , the code will show the expected output. We know the reason.
2. If I comment out only Line 2, the code is showing the expected output.
3. If I comment out only Line 1, EOFException is thrown at runtime. But why ?


Please let me know why Exception is thrown in the 3rd scenario. Thanks in advance.

( Jesper Young: Added code tags )
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ohhh, please use code tag buddy.

At line 1 you are writing you object to a file writing.ser, you can open and see the written object in bytes.
At line 2 you are reading same object from the same file.

Now if you comment line 1, then you will not write anything in your file. File will be blank.
Now if you read blank file, you will get java.io.EOFException.


SCJP 6
Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11
Thanks. But what about the first scenario, then ? If I comment out both Line 1 and Line 2, does not the code write anything to the file ?
The code gives expected output.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14192
    
  20

Please use code tags when you post code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Following code fragment is extracted from Banashri's code:




Both methods readObject and writeObject has private access. But if I replace "private" with "public", the code doesn't work. How can this be? How this is implemented by J2SE? I really confused with this for a long time. :?:
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Banashri Mandal wrote:Thanks. But what about the first scenario, then ? If I comment out both Line 1 and Line 2, does not the code write anything to the file ?
The code gives expected output.


Yes for the first scenario it neither writes anything to the file not reads from it.
It just prints what you have set to Desktop object at the starting of your program.

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
That is very advance concept Treimin, I only know private method writeObject() is called internally by
ObjectOutputStream.writeObject(Object) method. It must be using reflection inside and must be checking for private access of these methods.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Punit Singh wrote:That is very advance concept Treimin, I only know private method writeObject() is called internally by
ObjectOutputStream.writeObject(Object) method. It must be using reflection inside and must be checking for private access of these methods.


Can we use the reflection API to check the private access? :?:
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Do not know much about, as it is not in the scjp course, but I know using reflection you can make immutable String to mutable. Reflection is very powerful api.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
See this link's 1st and 2nd reply to get some idea about power of reflection.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Punit Singh wrote:See this link's 1st and 2nd reply to get some idea about power of reflection.


Thanks for the link
It seems we have to study about Reflection API too.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Why we have to read, it is not in the scjp course :roll:
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Punit Singh wrote:Why we have to read, it is not in the scjp course :roll:


Oh! For me, I've already completed SCJP. (But don't ask why am I here then )
In addition to SCJP, we have to learn about lot of API and frameworks (in future).
Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11

Hi all,

I appeared SCJP 1.5 last monday, and got 98%.

Thanks to this site which allowed me to share my doubts while preparing. Kathie Sierra book for SCJP preparation is the best book, which helped me most for the preparation.

Thanks to all of you.
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Congrats for an excellent score!!!
 
GeeCON Prague 2014
 
subject: Serialization