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
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

regarding Serializable

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,all
we know that being the implementation of Serializble requires the class having a no-arg constructor;
my understanding of the restriction is that in the time of the serialized object being deserialized, the no-arg constructor would be invoked to reconstruct the object initially, then the fields part being read from the stream and being built.
but in the following code

only one called could be seen in the sceen.
what did i miss out?

Regards,
James Du
[This message has been edited by James Du (edited July 29, 2001).]
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
There is no restriction on class which implements Serializable to have a no argument constructor.You may call the overloaded constructor, and still Serialization will work.
Consider the following code snipplet :

This should print :
<pre>
overloaded constructor called for serialization
100
200
Hello
</pre>

in the output.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 29, 2001).]
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, desai, thanks for your reply
I found the following quotes in the API


All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.



To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable in this case. The error will be detected at runtime.


here is my new doubt:
since making a class Serializable need no extra method to implement and a class is Serializable if its superclass is Serializable, why java designer not implicitly make class Object Serializable so all the classes become Serializable and need not to worry about the restrictioin?
Regards
James Du
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,
There are reasons for that.
Serialization allows you to create a JVM-independent binary representation of an in-memory Java object. This external representation may be used to transfer or store the object and to recreate it in another JVM.
The real power of serialization is the ability of Java programs to easily read and write entire objects and primitive data types, without converting to/from raw bytes or parsing clumsy text data
Since this process may involve moving( i mean saving) the state over the network or writing to a medium, it will be a costly process as a whole and not all class needs to implement it unless necessary
HTH
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for quick response


quoted by Ragu

Since this process may involve moving( i mean saving) the state over the network or writing to a medium, it will be a costly process as a whole and not all class needs to implement it unless necessary


well, i think the costly process could only take place in the time the object being serialized, if the object just created and stay still in the memory, there will no extra overhead.
Maybe there's the chance that JVM must do something special to support the feature? say, allocate a special sturcture space for the Serializable class in loading process?

Thanks in advance
James
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
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