Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Serialization, Heap and Objects

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, I was writing some sample code on serialization and testing out various parts of it. The code below is throwing me for a loop though because it doesnt make sense WHY something is the way it is. It may be a bug but I dont see how. Can somone explain my results?

Below you'll see that I have 6 boxes total. The first 3 which I create new instances of with new main(); The static variable boxes and the constructor of the class increment how many I have. These reference variables should be set each time the constructor is called on a new instance right? With me so far...

Now, we serialize these objects, and save them to a disk file. I read them in a different order back (because the SCJP study guide didnt mention it and I wanted to see if it worked the way I thought (which it did)). Regardless I read these into different "boxes" Box4, Box6 and Box5. Now I NEVER EVER created instances these objects. In fact they are declared as null.

Once the code reads the objects back in, theoretically it's creating an insance and pointing it to boxes 4,5,6. Here is the kicker. If it's creating new Main instances, why is the static variable not being incremented? If you run this code, the number of "boxes" returns 3. But in reality, you have 6 differnt boxes!

If the rule for statics is that it's the same once the class is "loaded" then I should have 6 right? Ok, I know what youre going to say... I cast it to a Main so therefore it's an Object instance not a Main instance. I'm cool with that BUT, if the generic "Object" doesnt contain my methods (not an IS-A) relationship, how did they get there! Ok, one more

System.out.println(Box6 instanceof Main);

Returns true!!!

I'm sure this is some special serialization thing but I'd like to know how that works. Even if it's not on the SCJP that's just creepy.

Thanks!

 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think I figured this out. On return of an object from serialization the constructor is NOT run and thus won't increment the static variable.

Bugger!!!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. The whole point of serialization is to store the current state of the object. Rerunning the constructor on the object would defeat the purpose.

If you modify your example by having Main extend Main1, and then creating the file Main1.java

,

you will see that for the three instances of Main that you read back in, something is happening.
 
Rob Mech
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Right. The whole point of serialization is to store the current state of the object. Rerunning the constructor on the object would defeat the purpose.



Exactly. I read that in the book too. What I think is great is that these sort of exercises help to reinforce the process. I answered my own question, it just had to click in.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, if the Class doesnot implement Serializable interface then at the deserialization time, the constructor will be invoked. Here class Main implements Serializable so no need to invoke constructor at the deserialization time.

--------------
Modified Code : to Explain When and when not Constructor will be invoked.
--------------



Try to avoid run this and see output and after that remove comment of SCJPParent Class and then run the code.

I hope this will help you little.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic