• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Banashri Mandal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post code.
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this link's 1st and 2nd reply to get some idea about power of reflection.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why we have to read, it is not in the scjp course :roll:
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats for an excellent score!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic