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

  • Home
  • SEARCH
  • 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 7940185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:18:48+00:00 2026-06-03T23:18:48+00:00

This question is about ObjectInputStream and how it builds fields declared as transient. Considering

  • 0

This question is about ObjectInputStream and how it builds fields declared as transient. Considering a simple use-case of ObjectInputStream

FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
SomeClass sc = (SomeClass) ois.readObject();

Where SomeClass is

class SomeClass {
   int x;
   transient OtherClass y;
}

class OtherClass {
   int z;
}

what will be the value of sc.y after ois.readObject ?

I am asking to clarify what i read at docs.oracle.com which states

“Fields declared as transient or static are ignored by the deserialization process. References to other objects cause those objects to be read from the stream as necessary.”

What does it mean that transient fields are ignored? And how can they be read from the stream if they are transient (ie not serialized – how i understand it…)

Matthias

  • 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-03T23:18:50+00:00Added an answer on June 3, 2026 at 11:18 pm

    what will be the value of sc.y after ois.readObject ?

    Someclass.y will be the “default value”. In this case, since it’s an object, it will be null.

    What does it mean that transient fields are ignored?

    They’re not serialised – they’re skipped. From the Java Object Serialisation Specification:

    A Serializable class must do the following:

    • Implement the java.io.Serializable interface
    • Identify the fields that should be serializable
      (Use the serialPersistentFields member to explicitly declare them serializable or use the transient keyword to denote nonserializable fields.)

    … and …

    The easiest technique is to mark fields that contain sensitive data as private transient. Transient fields are not persistent and will not be saved by any persistence mechanism. Marking the field will prevent the state from appearing in the stream and from being restored during deserialization. Since writing and reading (of private fields) cannot be superseded outside of the class, the transient fields of the class are safe.

    And your next question:

    And how can they be read from the stream if they are transient

    They’re not in the stream – so, actually, they’re not read. As mentioned above, the end up as the ‘default value’ for that type (null for objects, 0/false for primitives).

    “Fields declared as transient or static are ignored by the deserialization process.”

    For the most common case, they’re ignored by the serialisation process – and because of that, they’re not in the stream, and won’t be deserialised. However, if you changed the class subsequent to serialising the object so that a field that used to be serialised is now marked as transient, then the deserialisation process will ignore that value when it finds it in the stream.

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

Sidebar

Related Questions

After asking this question about authorization, I've added a new custom attribute in an
In this question about creating a new perl module , it was suggested to
After asking this question about interface fields in records I assumed that the following
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
I have seen this question about deploying to WebSphere using the WAS ant tasks.
Follow up to this question about GNU make : I've got a directory, flac
This is sort of the Java analogue of this question about C# . Suppose
JD Long helped me with this: question about manual annotation . But is it
A few days ago I asked this question about jquery ajax function invoking action

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.