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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:58:28+00:00 2026-05-25T21:58:28+00:00

This is a strange one, am wondering if this is by design or a

  • 0

This is a strange one, am wondering if this is by design or a compiler bug. Am using Sun Java 6 on a PC, but also seen with Sun Java 5 on a linux box.

Suppose I have a file on disk containing a serialized class. I then add a final field to the class and declare and initialize the field in one statement:

public final int newField = 2;

If I read in an object that was serialized before this field was added, this new field is given a default value of 0, which is correct. However, the compiler replaces occurrences of the field by the constant “2”. OTOH, if I instead initialize the new field inside of a constructor:

Data (int d) {
    this.oldField = d;
    this.newField = 2;
}

then the field does not get optimized away and everything works as expected.

More explicitly, here is my Main class:

import java.io.*;
public class Main {
    public static void main(String[] args) {
        try {       
            FileInputStream fis = new FileInputStream ("Data.txt");
            ObjectInputStream ois = new ObjectInputStream (fis);

            Data d = (Data)ois.readObject();
            ois.close();
            fis.close();

            d.print();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

and here is the Data class:

import java.io.*;
public class Data implements Serializable {
    private static final long serialVersionUID = 1;
    private int oldField;
    private final int newField = 2;

    Data(int d) {
        this.oldField= d;
    }

    public void print() {        {
        System.out.println(this.oldField + " " + this.newField);
    }
}

So my Data.txt file has a serialized instance of the original class with just “oldField = 1”. So the expected output of running this is

1 0

but instead I get

1 2

whereas if I move the initialization into the constructor the output is correct.

Is this expected behaviour? My feeling is that the compiler should not be optimizing away fields in serialized classes, precisely for this reason. Perhaps that is asking too much. Thanks!

  • 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-25T21:58:29+00:00Added an answer on May 25, 2026 at 9:58 pm

    This is by design. If a variable is declared as final, has a primitive type and an initializer which is a constant expressions, then it is a “constant variable”. This alters the semantics of initialization among other things. Specifically, the expression value is evaluated at compile time, and then embedded into the code.

    Refer to JLS 4.12.4 for details.

    When you put the initialization into the constructor, the variable is no longer a “constant variable” and normal initialization occurs.


    By the way, I would have thought that a final variable having a different value than what the initializer said was a bad thing, not a good thing. To me, that would be highly unintuitive. At any rate, relying on this kind of behaviour does not strike me as good practice.

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

Sidebar

Related Questions

Strange one this, which isn't programming related directly, but I thought it important to
This is a bit of a strange one, but I've been struggling for a
This is a strange one, I have everything working but in the controller code
This may seem strange... but I'm wondering if there is anyway to make a
Okay, this is a strange one. I'm trying to create a custom culture using:
I have never seen this issue before, its very strange. Just wondering if anyone
I promise you I am not lying. There is one strange line in this
This is a really strange issue. One day my project started to do a
I just come across this strange situation . I was using the technique of
This is a strange one Take this schema: Contact: actAs: [Timestampable,SoftDelete] columns: first_name: {

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.