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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:26:02+00:00 2026-05-30T16:26:02+00:00

This is from JLS 17.5: The usage model for final fields is a simple

  • 0

This is from JLS 17.5:

The usage model for final fields is a simple one. Set the final fields for an
object in that object’s constructor. Do not write a reference to the object being
constructed in a place where another thread can see it before the object’s constructor
is finished.
If this is followed, then when the object is seen by another thread,
that thread will always see the correctly constructed version of that object’s final
fields. It will also see versions of any object or array referenced by those final
fields that are at least as up-to-date as the final fields are.

The discussion in JLS 17.5 includes this sample code:

class FinalFieldExample {
    final int x;
    int y;
    static FinalFieldExample f;

    public FinalFieldExample() {
        x = 3;
        y = 4;
    }

    static void writer() {
        f = new FinalFieldExample();
    }

    static void reader() {
        if (f != null) {
            int i = f.x; // guaranteed to see 3
            int j = f.y; // could see 0
        }
    }
}

I tried reusing this code to replicate the situation above, and here is what I have:

public class FinalFieldThread extends Thread {

    public static void main(String[] args) {
        ThreadA threadA = new ThreadA();
        ThreadB threadB = new ThreadB();

        threadB.start();
        threadA.start();
        //threadB.start();

    }
}

class ThreadA extends Thread {

    @Override
    public void run() {
        System.out.println("ThreadA");
        FinalFieldExample.writer();
    }
}

class ThreadB extends Thread {

    @Override
    public void run() {
        System.out.println("ThreadB");
        FinalFieldExample.reader();
    }
}

I can test how final gets read correctly, but how can I replicate when it is not read correctly (i.e. when there is a reference to the tread before the constructor is finished?)

  • 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-30T16:26:03+00:00Added an answer on May 30, 2026 at 4:26 pm

    What you are looking for

    What you are trying to test is called Don’t publish the “this” reference during construction or Visibility Hazard. Read the following links in the order they are provided.

    Reading

    1. Java theory and practice: Safe construction techniques
    2. JSR 133 (Java Memory Model) FAQ
    3. Do the ‘up to date’ guarantees for values of Java's final fields extend to indirect references?

    Sample Code

    class FinalField
    {
        final int x;
        int y;
    
        public FinalField()
        {
            Thread t = new Thread(new TestThread(this));
            t.start();
    
            y = 4;
            x = 3;
        }
    }
    
    class TestThread implements Runnable
    {
        FinalField f;
        TestThread(FinalField f)
        {
            if(f.x != 3)
                System.out.println("value of x = " + f.x);
    
            this.f = f;
        }
    
        public void run() 
        {
            if(f.x != 3)
                System.out.println("value of x = " + f.x);
        }
    }
    
    public class Test
    {
        public static void main(String[] args) 
        {
            for(int i=0; i<100; i++)
            {
                new FinalField();
            }
        }
    }
    

    Output

    value of x = 0
    value of x = 0
    value of x = 0
    .
    .
    .
    value of x = 0
    value of x = 0
    value of x = 0
    

    Explanation of the output

    When I access the final field in the constructor of my thread then at that time the final field x was not properly initialized, and that’s why we are getting 0. Whereas when I access the same field in the run() then by that time the final field x is initialized to 3. This is happening because of the escaping of the reference to the object of FinalField. Read the 1st link I have shared, it is much more detailed.

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

Sidebar

Related Questions

Say I write this: from subprocessing import Popen, STDOUT, PIPE p = Popen([myproc], stderr=STDOUT,
I have HQL like this: from Table1 t1 where t1.name not in (select t2.name
Specifically I have code that simplifies to this: from datetime import datetime date_string =
I write this from in Codeigniter as a form_view.php . <?php echo form_open('form'); ?>
I am cross-posting this from scala-user: I have the following: object XmlTest { import
I have edited this from my real code, so that it is a little
I got this from one test and really didn't get the point. Which one
I've adapted this from an example that I found on the 'net... function ratio($a,
This (from body of a stored proc) is throwing a syntax error: IF (name
I see this from time to time and want to know what it is.

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.