Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

static variables and serialization

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My source is Sun Certification Sample

I know serialization is not in the exam anymore but I want to understand whether the reported answer is right.


The reported answer is 0 7 0 but my understanding is that it should be 7 7 0 as the value of the static variable is going to remain unaffected by serialization/unserialization as it is not part of serialized object. Is it an error in the question or is there a way of interpreting the question so the given answer makes sense? Incidentally, I have included the serialization/deserialization code as the original just stated:

4) Given:
2. import java.io.*;
3. public class Network {
4. public static void main(String[] args) {
5. Traveler t = new Traveler();
6. t.x1 = 7; t.x2 = 7; t.x3 = 7;
7. // serialize t then deserialize t
8. System.out.println(t.x1 + " " + t.x2 + " " + t.x3);
9. }
10. }
11. class Traveler implements Serializable {
12. static int x1 = 0;
13. volatile int x2 = 0;
14. transient int x3 = 0;
15. }

If, on line 7, t is successfully serialized and then deserialized, what is the result?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, You are right output will be "7 7 0"
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was pretty upset about the low quality of the sample questions and proposed "correct" answers - which aren't even free. The proposed answer and explanation from your example can only be created of by a total lack of understanding how a simple program runs in general, combined the laziness to not test it.

Maybe the serialization questions in the real exam were all wrong, too, and that's why they had to remove them?

Kai
 
Ranch Hand
Posts: 238
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not impressed either... well, at least it's been corrected here
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!