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 6351101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:56:14+00:00 2026-05-24T21:56:14+00:00

The Java serialization spec for Java 1.5 said: For serializable objects, the no-arg constructor

  • 0

The Java serialization spec for Java 1.5 said:

For serializable objects, the no-arg constructor for the first
non-serializable supertype is run. For serializable classes, the
fields are initialized to the default value appropriate for its type.
Then the fields of each class are restored by calling class-specific
readObject methods, or if these are not defined, by calling the
defaultReadObject method. Note that field initializers and
constructors are not executed for serializable classes during
deserialization.

However, this means if we put a static variable (for example a counter variable) inside the class, it will not be updated as normally would:

class Foo {
    static int t;

    public Foo() {
        t++;
    }
}

public class Bar extends Foo implements Serializable {
    static int t;

    public Bar() {
        t++;
    }
}

In this case, if one instance of Bar is deserialized, then the counter for Foo is correct and the counter for Bar is off-by-one.

I wonder why does deserialization does not invoke the constructor? Since it seems that while this will gain a bit on speed, it can cause potential problems. The compiler could be easily designed to produce a “static constructor” that only updates the static variables that will be updated and does not rely on outside information when the class is loaded.

Also, I wonder what is the best way to avoid this? The solution I can think of is packing the deserialization with the operation on the static variable.

Thanks for any inputs in advance!

  • 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-05-24T21:56:15+00:00Added an answer on May 24, 2026 at 9:56 pm

    Deserialization doesn’t invoke the constructor because the purpose of it is to express the state of the object as it was serialized, running constructor code could interfere with that.

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

Sidebar

Related Questions

I have a few Java classes that implements Serialization, and corresponding AS objects (self-generated
In a java serialization problem, I want to save some classes name and I
I know that Serialization (Serializable) is not available in the Micro Edition of Java.
Can anyone tell me what is the need of Serialization of objects in Java
Although the java.io.Serializable interface public interface Serializable{} surprisingly doesn't contain any methods and fields
I've been lately trying to learn more and generally test Java's serialization for both
What is the Java analogue of .NET's XML serialization?
What is the difference between serialization and synchronization in java? I need an explanation
Kryo is a very new and interesting Java serialization library, and one of the
What is the best procedure for storing and retrieving, using native Java serialization, generic

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.