Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:32:23+00:00 2026-06-14T11:32:23+00:00

From the SCJP 6 study guide – there is a question asking for the

  • 0

From the SCJP 6 study guide – there is a question asking for the output of the following code regarding serialization:

import java.io.*;

public class TestClass {
  static public void main(String[] args) {
    SpecialSerial s = new SpecialSerial();
    try {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
        os.writeObject(s);
        os.close();
        System.out.print(++s.z + " ");
        s = null;  // makes no difference whether this is here or not

        ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
        SpecialSerial s2 = (SpecialSerial)is.readObject();
        is.close();
        System.out.println(s2.y + " " + s2.z);
    } catch (Exception e) {e.printStackTrace();}
  }
}
class SpecialSerial implements Serializable {
    transient int y = 7;
    static int z = 9;
}

The output of this is: 10 0 10

The reason given for this is that the static variable z is not serialized, which I would not have expected it to be.

The value of the static int variable z is incremented to 10 after the object has been written to file, in the println() statement.

This being the case, why is it not back to either it’s original value of 9 when the class is deserialized, or as the class is not being created in the normal way, the class default int value of 0, rather than remaining with it’s non-default incremented value of 10 after deserialization? I would have thought the value of it being 10 would be lost, but this is not the case.

Anyone shed some light? I’m stumbling around here in the dark stubbing my toes on this one..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:32:24+00:00Added an answer on June 14, 2026 at 11:32 am

    Basically, instances are serialized, not classes. Any static fields declared by the class are unaffected by serialization/deserialization of an instance of the class. For z to be reset to 9, the SpecialSerial class would need to be reloaded, which is a different matter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The SCJP 6 Study Guide from Bert Bates and Kathy Sierra states on page
Below is a question from Kathy & Bert Bates SCJP 5 preparation CD. I
Background info I have a query regarding a questions from Sierra & Bates, SCJP
This code is taken from a SCJP practice test: 3. public class Bridge {
This question was taken from Kathy Sierra SCJP 1.6 . How many objects are
I have the following Java code: public String makinStrings() { String s = Fred;
From the SCJP Questions PDF book, I got this question.. 1. enum Animals {
From 19 Deadly Sins of Software Security ; The following code is the poster
It's actually from SCJP 6 StudyGuide Exam310-065(MGH,2008) alt text http://www.freeimagehosting.net/uploads/1f37466409.png alt text http://www.freeimagehosting.net/uploads/03aaff252a.png alt
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.