aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serializing HashMap 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 "Serializing HashMap" Watch "Serializing HashMap" New topic
Author

Serializing HashMap

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 357
I am trying out the following piece of code.


The above code works fine. My question is that though i have put a non-serialized object in HashMap (new Object()), how is it that the above code does not throw java.io.NotSerializableException.

If i replace o.writeObject(m); with o.writeObject( new Object()); i get java.io.NotSerializableException.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Naresh Chaurasia wrote:The above code works fine.

Really ?? Which jdk are you using ?


[My Blog]
All roads lead to JavaRanch
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


Hi Naresh,

I tried your code and its throwing Exception as expected .






Now its really a question how its working for you ?

Thanks
Javin


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 357
Try the following program


I also found something else.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 357
I was making a mistake...I understood what was the problem. My constructor was never getting called, so this problem occured. Thanks any way.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

This is completely different. You're not making any new instance of SerializingHashMap, so your map is empty. That's why it works ! AvoidRedHerrings next time.
 
Don't get me started about those stupid light bulbs.
 
subject: Serializing HashMap